I want to bind database value to combo box in C# windows form. I know this is a common problem for begginers. I search in the internet about this. Acoording to that I modified my code. But my problem is, it is not raised any error message and data not fill to combo box. (Mentioned database table has values. It is not empty) This is my code,

         ds = commm.GetDataSet("SELECT * FROM StudentTable");//Get dataset (This is working well)
        comboBox1.DataSource = ds.Tables[0];
        comboBox1.DisplayMember = "Stu_Name";
        comboBox1.ValueMember = "Stu_ID";

It is not loaded data to combo box.

Recommended Answers

All 5 Replies

If my memory serves right,
for the following code,

string sqlCommand = "SELECT * FROM TABLE";
string connectionString = "blahblah";

DataSet = GetDataSet(sqlCommand,connectionString);

GetDataSet()
{
   //...?
}

you need to code something like the following

public DataSet GetDataSet(string ConnectionString, string SQL)
{
    SqlConnection conn = new SqlConnection(ConnectionString);
    SqlDataAdapter da = new SqlDataAdapter();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = SQL;
    da.SelectCommand = cmd;
    DataSet ds = new DataSet();

    conn.Open();
    da.Fill(ds);
    conn.Close();

    return ds;
}

Hope this helps you...

Have a happy coding...:D

modify you code as below.

ds = commm.GetDataSet("SELECT * FROM StudentTable");//Get dataset (This is working well)
        comboBox1.DisplayMember = "Stu_Name";
        comboBox1.ValueMember = "Stu_ID";
  comboBox1.DataSource = ds.Tables[0];

if it is not working you then you need to use data adapter as ss125 given the solution.

Thank you for your reply both ss125 and inaaam.
inaam, I tried what you say. It doesn't work.
ss125, i got my data set using ur function.
this is what in my code.

ds = commm.GetDataSet("SELECT * FROM StudentTable");`

commm is a object of my common class and getDataset is a funtion which return dataset. DS working well. But combo box is not loaded data.

k can you try and tell using the following query.. Its for a test purpose.

ds = commm.GetDataSet("SELECT Stu_ID FROM StudentTable");

There are null values in my DB Stu_Name. Sorry for the inconvineance. My Originaly posted code work well. Thank you everyone for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.