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.

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

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

Try this

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

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

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

Thanks alot LOL my code works also

I tested it just to make sure :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.