Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fill DropDownList from database in asp.net?

how to fill DropDownList from database in asp.net ?

and when i pick value from the DropDownList how to catch this event ?

Conn.Open();
SQL = "SELECT distinct city FROM MEN";
dsView = new DataSet();
adp = new SqlDataAdapter(SQL, Conn);
adp.Fill(dsView, "MEN");
adp.Dispose();

DropDownList1. ?????? (what do to ?)

thanks in advance

like image 954
Gold Avatar asked Dec 01 '25 08:12

Gold


2 Answers

You set the DataSource, DataTextField and DataValueField and call DataBind() in order to populate the dropdownlist.

The datasource can be pretty much any IEnumerable and the text and value will be looked up with reflection.

The event you want to catch is the SelectedIndexChanged event - this will fire when you change the selection.

like image 147
Oded Avatar answered Dec 03 '25 20:12

Oded


First take the details in a dataset then the following code will help you:

DropDownList1.DataSource = ds
DropDownList1.DataTextField = "emailid"
DropDownList1.DataValueField = "userid"
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, New ListItem("select", "-1"))
like image 22
Nimitha madhavram Avatar answered Dec 03 '25 21:12

Nimitha madhavram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!