Fill ASP.Net Dropdown List From Database Table Using ASP.NET C#


Sometimes their is a need to fill a Drop Down List from a database table. For that many beginners get into trouble trying to fill the DropDownList from database table. so by considering above requirement i have written this article for beginners, students and anyone who wants to learn how to fill a DropDownList from a database table.
Now let us start by creating a table named employee in the database as in:
I hope you have created the employee table in your database. Now insert records into the table as:
  1. insert into  employee (FirstName) values ('vithal')  
  2. insert into  employee  (FirstName) values ('Sudhir')  
  3. insert into  employee  (FirstName) values ('virat')  
  4. insert into  employee  (FirstName) values ('Pravin')  
  5. insert into  employee  (FirstName) values ('Mayank')  
Now let us start to create a Website as:
  1. Open Visual Studio from Start - - All programs -- Microsoft Visual Studio.
  2. Then go to to "File" -> "New" -> "WebSite..." then select Visual C# -> Web application.
  3. After that specify the name i.e Fill_dropdownlist or any name as you wish and the location of the project and click on the OK button. The new web site is created.
Use the following source code in the defualt.aspx <body> section page as:
  1. <body bgcolor="#ccccff">  
  2.     <form id="form1" runat="server">  
  3.     <div class="div" align="center">  
  4.     <br /> <br />  
  5.         <asp:DropDownList ID="DropDownList1" runat="server" Width="100px">  
  6.         </asp:DropDownList>  
  7.     </div>  
  8.     </form>  
  9. </body>  
Then switch to view code and use  the following code in the  page load as:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     string constr = ConfigurationManager.ConnectionStrings["binddropdown"].ToString();   
  4.     // connection string  
  5.     SqlConnection con =new SqlConnection (constr);  
  6.     con.Open();  
  8.     SqlCommand com=new SqlCommand ("select *from employee",con);   
  9.     // table name   
  10.     SqlDataAdapter da=new SqlDataAdapter(com);  
  11.     DataSet ds =new DataSet ();   
  12.     da.Fill(ds);  // fill dataset  
  13.     DropDownList1.DataTextField = ds.Tables[0].Columns["FirstName"].ToString(); // text field name of table dispalyed in dropdown       
  14.     DropDownList1.DataValueField=ds.Tables[0].Columns["id"].ToString();    
  15.     // to retrive specific  textfield name   
  16.     DropDownList1.DataSource=ds.Tables[0];      //assigning datasource to the dropdownlist  
  17.     DropDownList1.DataBind();  //binding dropdownlist  
  18. }
Now run the application and select a value from the DropDownList which looks as in the following image:
I hope this article is useful for all readers. If you have any suggestions then please contact me.
  • Please download the zip file of source code
  • Make the changes in the web.config file according to your server location

Similar Articles