In previous post i explained how to bind data to grid view in asp.net,bind data to dropdownlist in Asp.net mvc,Gridview paging and sorting,Jquery dropdownlist selected value.Here i will describe how to bind the data to drop down list in asp.net.The "ddlcity is id of the drop down list.The function "Loadddlcity()" which is return the data of city as data table.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Bind data to dropdownlist in asp.net</title> </head> <body> <form id="form1" runat="server"> <asp:DropDownList ID="ddlOrdr" runat="server"> </asp:DropDownList> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Data.SqlClient; using System.Data; public partial class DropdownBind : System.Web.UI.Page { SqlConnection Orcon = new SqlConnection(ConfigurationManager.AppSettings["OrdrCon"].ToString()); protected void Page_Load(object sender, EventArgs e) { dTable =Loadddlcity(); ddlOrdrcity.DataTextField = "cityname"; ddlOrdrcity.DataValueField = "cityid"; ddlOrdrcity.DataSource = dTable; ddlOrdrcity.DataBind(); public DataTable Loadddlcity() { con.Open(); try { oda = new SqlDataAdapter("select*from city",Orcon); oda.Fill(ods, "citymaster"); return ods.Tables["citymaster"]; } catch { throw; } finally { cmd.Dispose(); Orcon.Close(); Orcon.Dispose(); } }
No comments:
Post a Comment