how to fill drop down list in from database.

Total Post:341

C#  ASP.Net 
 1030  View(s)
Rate this:
i want fill drop down list from database in my application. please give me any solution.
  1. Post:282

    Re: how to fill drop down list in 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

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

    SqlConnection con = new SqlConnection(constr);


    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

      Modified On Apr-10-2018 05:04:28 AM