5
Answers

Retrieve Data from Database into comboBox in C#

Bineesh  VP

Bineesh VP

10y
29.4k
1

Sir, I am doing a program of comboBox filling from database.

I need your help in suggesting a higher C# standard code for this function.

Please go through all the code. I need a capsulized code and a higher standard code.

please tell me how this code change to standard and precise.

Here the Code I  worked with:-


1) SQL Table:-


empId    numeric(18, 0)    Unchecked
empName    varchar(20)    Unchecked
Age    int    Unchecked
address    varchar(MAX)      Unchecked
department    varchar(20)    Unchecked
salary    int    Unchecked       Unchecked


2) SQL Stored Procedure:-


ALTER PROCEDURE employeeViewAll
    
AS
       
SELECT     empName, Age, address, department, salary
FROM        tbl_Employee RETURN 



3) Class SP function:-

public DataTable EmployeeViewAll()
    {
        try
        {
            DataTable dtbl = new DataTable();
            SqlDataAdapter SqlDa = new SqlDataAdapter("employeeViewAll", sqlCon);
            SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            SqlDa.Fill(dtbl);
            return dtbl;
        }
        catch (Exception)
        {
            throw;
        }
    }


4) form.cs Code:-


public void ComboFill()
        {
            DataTable d = new DataTable();
            eSP SP = new eSP();
            d = SP.EmployeeViewAll();
            comboBox1.DataSource = d;
            comboBox1.DisplayMember = "department";
        }


5) Finally, I called the function in form load:-



private void frmTest_Load(object sender, EventArgs e)
        {
            ComboFill();
        }

Please help me in this topic.
Answers (5)