0

I want to populate a drop down list fetching data from database along with a "select" listitem at the top of the srop down list.

I am using dropdownlistname.items.add("select","0")
and dropdownlistname.items.insert("select","0")
and then i am fetching data from database in a dataset and then binding the data into the drop down list. But then i am seeing the data from the database only, i am not seeing the "select" listitem.

Pls help to include the select item at the top of the list.

5
Contributors
5
Replies
7
Views
7 Years
Discussion Span
Last Post by Freon22
0

I don't like to use datasets and bindings. Try with next code, ORDER BY clause is if you want to sort data from table:

Dim connection As New SqlConnection(connectionString)
Dim command As New SqlCommand("SELECT * FROM table ORDER BY collumn ASC", connection)
Dim reader As SqlDataReader

Try
   connection.Open()
   reader = command.ExecuteReader(CommandBehaviour.Default)

   While reader.Read()
      dropDownList.Items.Add(reader("collumn"))
   End While

   connection.Close()
Catch ex As Exception
   
End Try

Edited by jugosoft: n/a

0

I am using dropdownlistname.items.add("select","0")
and dropdownlistname.items.insert("select","0")

Try this

dropdownlistname.Items.Insert(0, "<-- Select -->");

0

Hello Subhankar,

If your problem is not yet solved you can try this code..It will surely work :

DropDownListName.Items.Insert(0, new ListItem("--Select--","0"));

Thanks,
Rohan

0

you can use the code provided by laghaterohan, but you have to put this code after you bind the data from database.

Edited by Drakoo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.