HOW TO FILL DROP DOWN LIST IN ASP.NET FROM DATABASE.

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  ASP.Net 
 653  View(s)
Ratings:
Rate this:
i want fill drop down list from database in my asp.net application. please give me any solution.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to fill drop down list in asp.net from database.

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!