Introduction
This article will be explain how to bind database table data with Dropdownlist control in Asp.net. There are many way by which you can bind table data with Dropdownlist control.
Firstly go in Dropdownlist properties window and set properties 'True' of AutoPost Back other wise data will not be displayed in Dropdownlist.
Method 1
This article will be explain how to bind database table data with Dropdownlist control in Asp.net. There are many way by which you can bind table data with Dropdownlist control.
Firstly go in Dropdownlist properties window and set properties 'True' of AutoPost Back other wise data will not be displayed in Dropdownlist.
Method 1
protected
void
Page_Load(object
sender,
EventArgs
e)
{
SqlDataAdapter
da;
DataSet
ds;
da =
new
SqlDataAdapter("select
* from Table",
"Data Source=.;Initial Catalog=san;Integrated Security=True");
ds =
new
DataSet();
da.Fill(ds);
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField =
"name";
DropDownList1.DataValueField =
"id";
DropDownList1.DataBind();
}
Method 2
public
partial
class
_Default
: System.Web.UI.Page
{
SqlDataReader
dr;
SqlCommand
cmd;
SqlConnection
con;
protected
void
Page_Load(object
sender,
EventArgs
e)
{
con =
new
SqlConnection("Data
Source=.;Initial Catalog=san;Integrated Security=True");
cmd =
new
SqlCommand("select
* from Table");
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
while
(dr.Read())
{
DropDownList1.Items.Add(new
ListItem(dr.GetString(1),
dr.GetString(0)));
}
}
}