Loading sql table coulmn valus into combo box in c#

To bind or to load a sql table column value into combo box first of all u create object of SqlConnection class which is located in System.Data.SqlClient namespace
and pass connection string into it

SqlConnection con=new SqlConncetion("data source=your_server_name;initial catalog=your_database_name;user id=used_name;password=server_password";

then create object of SqlCommand object and pass your sql query to retrive or to load table column value into combo box:
And Event on form Load event

string query="select test from TestApp";
SqlCommand cmd=new SqlCommand(query,con);
cmd.CommandType = CommandTypeText;

create a object of SqlDatareader to read or fetch the column values

SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())//while true
{
query=dr[0].ToString();
combobox1.Items.Add(query);//loading values into combo
}
dr.Close();//closing the datareader
con.Close();//closing connection


Complete code snippet:

SqlConnection con=new SqlConncetion("data source=your_server_name;initial catalog=your_database_name;user id=used_name;password=server_password";

//Form Load Event
string query="select test from TestApp";
SqlCommand cmd=new SqlCommand(query,con);
cmd.CommandType = CommandTypeText;
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())//while true
{
query=dr[0].ToString();
combobox1.Items.Add(query);//loading values into combo
}
dr.Close();//closing the datareader
con.Close();//closing connection

Thankuin you
Ebook Download
View all
Learn
View all