Bind Dropdown List Using SQL Data Source Without Code Backend Page In ASP.NET

Today, I have provided an article, which exhibits how to bind a dropdown list with a database without a backend code page in ASP.NET. In this blog, we will create a table in a SQL Server database and bind the table with a dropdown list control. This is an easy way to bind a dropdown list, using the database values in an HTML page. Here, I will write a SQL query in Sqldatasource. There is no need to use the code backend page.

Step 1 
 
Webconfig file 
  1. <connectionStrings>  
  2.     <add name="business" connectionString="Data Source=RAM;Initial Catalog=db_business; providerName=" System.Data.SqlClient "/>  
  3.   
  4. </connectionStrings>  

Step 2 

Aspx code 
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title></title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <asp:DropDownList ID="ddlLocation" CssClass="form-control" runat="server" DataSourceID="dsLocation" DataTextField="fld_location" DataValueField="fld_id">  
  11.                 <asp:ListItem Selected="True">--SELECT--</asp:ListItem>  
  12.             </asp:DropDownList>  
  13.             <asp:SqlDataSource ID="dsLocation" runat="server" ConnectionString="<%$ ConnectionStrings:business %>" SelectCommand="SELECT fld_id,fld_location FROM dbo.tbl_LOCATION"></asp:SqlDataSource>  
  14.         </div>  
  15.     </form>  
  16. </body>  
  17.   
  18. </html>   

Output

 
Ebook Download
View all
Learn
View all