Retrieve Data from Database into comboBox in C#
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.

Hemant SrivastavaPosted Oct 2, 2013, 3:32 PM
public System.Data.DataTable EmployeeViewAll()
{
DataTable dtbl = new DataTable();
try
{
// Here it shuld be your database Connection String
string connectionString = "Server = .; database = HKS; Integrated Security = true";
using (SqlConnection sqlCon = new System.Data.SqlClient.SqlConnection(connectionString))
{
SqlDataAdapter SqlDa = new SqlDataAdapter("employeeViewAll", sqlCon);
SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
SqlDa.Fill(dtbl);
}
return dtbl;
}
catch (Exception)
{
throw;
}
}
public void ComboFill()
{
DataTable dt = new DataTable();
eSP SP = new eSP();
d = SP.EmployeeViewAll();
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "department";
comboBox1.ValueMember = "empName";
}
santhosh kumarPosted Nov 4, 2014, 9:07 AM
Bineesh ViswanathPosted Oct 4, 2013, 11:11 PM
Jignesh TrivediPosted Oct 3, 2013, 12:28 AM
only the suggesion is mention Value member of combo box and
select only those value which is really required from the data base...
Hemant SrivastavaPosted Oct 2, 2013, 3:19 PM
There are some issues in your code:
(1) SQL connection is missing in EmployeeViewAll()
(2) ComboBox generally have two things: One is any item (which you see on the comboBox list) and another is its value ( which you would use in your program after selecting any item in the list)
For example:
Suppose you have
-----------------------------
Department empName
-----------------------------
Civil Robert
Medical Sozy
Computer Jim
Chemistry Rahul
Physics Kumar
After opening ComboBox List, you would see Civil,Medical,Computer,Chemistry,Physics (i.e. items)
On selecting say item 'Computer' in the list, its itemValue will be Jim'
So you should have something like in ComboFill() in order to bind data source:
comboBox1.DisplayMember = "department";
comboBox1.ValueMember = "empName";
But ValueMember can be only one value not more than one.