Adding “Select One” inside dropdown list

Q. How to add “Select One” item inside dropdown list while populating dropdownlist from database?
Q. First item selection problem – no event fire on select index changed but it works for second item.

Below is the code to add “Select One” option as first item inside dropdown list.
Here BindDropDownList1() is the function to bind dropdown list from database.
private void BindDropDownList1()
string strConn = “server=(local);uid=UID;pwd=PWD;database=Northwind”;
string MySQL = “Select LastName from Employees”;
SQLConnection MyConn =new SQLConnection(strConn);
SQLDataReader objDR ;
SQLCommand Cmd =new SQLCommand(MySQL, MyConn);
DropDownList.DataSource = objDR;
DropDownList.Items.Insert(“0”,new ListItem(“Select One”,”0″));

Now your dropdown list looks like:

[Note: the last line DropDownList.Items.Insert(“0”,new ListItem(“Select One”,”0″)); add “Select One” item in your dropdown list and its index is “0”.
Now when the Page Load and dropdown list fill with all the data from database as well as with “Select One” item and “Select One” is selected as your first item.
Now the event is also fire on select index changed for the first item that comes from your database.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: