Hoang Minh
Hoang Minh

Reputation: 1220

How to retrieve data from sql server and put into a combo box using C#?

I have a SQL database table create on Visual Studio. Now I want to access that database, read its value based on a certain column and save those value into the combo box list.

For example, if I have a table like this

|StudentName    | Age    | ID    |
|---------------|--------|-------|
|A              | 19     | 1     |
|---------------|--------|-------|
|B              | 15     | 2     |
|---------------|--------|-------|
|C              | 20     | 3     |
|---------------|--------|-------|

and a combo box named nameCombo, what I want is to have something like this

nameCombo.Items.Add(A);
nameCombo.Items.Add(B);
nameCombo.Items.Add(C);

How am I going to do this ? Thanks.

EDIT

Assuming that you have already connect LINQ to your SQL database. Here is what you should do to update your combo box. All thanks to those awesome guys here in stackoverflow.

locationLinqToSQLDataContext db = new locationLinqToSQLDataContext();
var nameData = from name in db.Locations
               select new { name.StudentName };

foreach (var name in nameData)
    {
        fromTextBox.Items.Add(name.StudentName);
    }            

Upvotes: 1

Views: 3036

Answers (3)

Tonkleton
Tonkleton

Reputation: 547

Here is a solution that uses SqlConnection (which naively assumes you don't need data binding). Of course you must replace "YourConnectionString" and "YourTable" with real values.

string connectionString = "YourConnectionString";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    string query = "SELECT Name FROM YourTable";
    SqlCommand command = new SqlCommand(query, connection);
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            // "0" refers to the 0th column in the result set.
            nameCombo.Items.Add(reader.GetString(0));
        }
    }
}

Upvotes: 1

TechneWare
TechneWare

Reputation: 263

Ok assuming LINQ then:

Query it out into a list.

var myData = (from d in db.MyTable
              where d.Name.contains("A")
              select d).ToList();

Then assign it to the combo:

mycombo.datasource=myData;
mycombo.dataTextField="Name";
mycombo.dataValueField="ID";
mycombo.dataBind();

Just made this up on the fly but it should work.

OR If you really want to iterate the items.

myData.foreach(delegate(MyTableItem i) 
{
    mycombo.add(new listitem(i.id,i.name));
});

Upvotes: 3

mybirthname
mybirthname

Reputation: 18127

If you need it for windows form application here is the code:

       DataTable dt = new DataTable("dataTable");

        dt.Columns.Add("Id", typeof(int));
        dt.Columns.Add("Name", typeof(string));
        //add DataRow
        DataRow row = dt.NewRow();
        row["Id"] = 1;
        row["Name"] = "One";

        dt.Rows.Add(row);
        //assign to ComboBox
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "Id";

For web applications you can see the other answer

Upvotes: 2

Related Questions