Bineesh  Viswanath

Bineesh Viswanath

  • NA
  • 1k
  • 761.6k

Retrieve Data from Database into comboBox in C#

Oct 2 2013 8:39 AM

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)