.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Data Populate in DropDownList through Sql Server

Posted By:Shashi Ray       Posted Date: September 02, 2008    Points: 10    Category: ADO.NET    URL: http://www.dotnetspark.com  

Data Populate in DropDownList through Sql Server Using ADO.Net
 

This sample code is in c#

string str="Sql Query";

Sqlcommand cmd=new Sqlcommand(str,con);

DropDownList1.DataTextField="Field1";

DropDownList1.DataValueFiels="Field1_code";

con.Open();

DropDownList1.DataSource = cmd.ExecuteReader();

DropDownList1.DataBind();

con.Close();

     

Further Readings:

Responses
Author: Mohan Raut         Company URL: http://www.dotnetspark.com
Posted Date: September 08, 2008

another way to fill dropdownlist from aspx page without code....
<asp:DropDownList ID="ddlCountry" DataSourceID="SqlDataSourceddlCountry" runat="server" DataTextField="CountryTitle" DataValueField="CountryId"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceddlCountry" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CountryId], [CountryTitle] FROM [Country]"></asp:SqlDataSource>

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend