Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all,
i want to show a column rows in a combobox. and i just can put one cell of table in a combobox. what i must do? in the other hand i want to show first row of column1,second row of column1,third row of column1 and ...

thank you all.
Posted

It's pretty simple though,
C#
SqlConnection con = new SqlConnection("DatabaseConnectionString");
SqlCommand cmd = new SqlCommand("SELECT SomeColumn FROM SomeTable", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

foreach(DataRow dr in dt.Rows)
{
    ComboBox1.Items.Add(dr[0].ToString());
}

-KR
 
Share this answer
 
See example:
using (SqlConnection sqlConnection = new SqlConnection("connectionString"))
{
    SqlCommand sqlCmd = new SqlCommand("SELECT column1 FROM tablename", sqlConnection);
    sqlConnection.Open();
    SqlDataReader sqlReader = sqlCmd.ExecuteReader();

    while (sqlReader.Read())
    {
        comboBox1.Items.Add(sqlReader["column1"].ToString());
    }

    sqlReader.Close();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900