Hi all,
How to bind DropDownList with SQL Server database ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Sep 25, 2011, 1:28 PM
Try this
SqlConnection con = new SqlConnection(strConnString);
if (!IsPostBack)
{
con.Open();
str = "select columnname from tablename";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
DropDownList1.Items.Add(reader["columnname"].ToString());
}
reader.Close();
con.Close();
}
Thanks
If this post helps you mark it as answer
Mayur DighePosted Sep 20, 2011, 1:03 PM
You can do it, by simply setting some properties of DropDownList Control.
If you going to code it for binding then set put below snippet in your code:
DropDownList1.DataSource = //data from DataSet, DataReader etc...
DropDownList1.DataTextField = "FieldName";
DropDownList1.DataValueField = "FieldName";
// this requires to display data properly in web applications
DropDownList1.DataBind();
If you want to go through wizard then just click the triangle symbol pop-up when you select the DropDownList Control. And simply follow the instructions displayed step - by - step.