Home > DeveloperSection > Forums > how to fill drop down list in asp.net from database.
Ankit Singh

Total Post:341

Points:2389
Posted on    October-30-2015 11:07 PM

 .NET C#  ASP.Net 
Ratings:


 1 Reply(s)
 403  View(s)
Rate this:
i want fill drop down list from database in my asp.net application. please give me any solution.


aditya kumar Patel

Total Post:250

Points:1782
Posted on    October-30-2015 11:32 PM

Use the DropDownList control to create a single selection drop-down list control. You can control the appearance of the DropDownList control by setting the BorderColor, BorderStyle, and BorderWidth properties.

To specify the items that appear in the DropDownList control, place a ListItem element for each entry between the opening and closing tags of the DropDownList control. It is server side control in asp.net.

string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString(); // connection string

SqlConnection con = new SqlConnection(constr);

con.Open();

SqlCommand com = new SqlCommand("select *from Employee", con); // table name

SqlDataAdapter da = new SqlDataAdapter(com);

DataSet ds = new DataSet();

da.Fill(ds); // fill dataset

DropDownList1.DataTextField = ds.Tables[0].Columns["Emp_Name"].ToString(); // text field name of table dispalyed in dropdown

DropDownList1.DataValueField = ds.Tables[0].Columns["Emp_ID"].ToString(); // to retrive specific textfield name

DropDownList1.DataSource = ds.Tables[0]; //assigning datasource to the dropdownlist

DropDownList1.DataBind(); //binding dropdownlist


Don't want to miss updates? Please click the below button!

Follow MindStick