We help IT Professionals succeed at work.

ASP.net DropDown loading issue

Murray Brown
Murray Brown asked
on
Hi

I am using the following code to add items to a DropDownList.
I use the lines
   Me.Category1.SelectedIndex = -1
            Me.Category1.Text = ""
to unselect the item that is automatically selected but this doesn't seem to work

    Sub Fill_Category1_Combobox()
        Try

            Dim sSQL As String
            sSQL = "SELECT DISTINCT [Segment 1 Desc] FROM Accounting"

            Dim connection As New SqlConnection(Session("MyVar"))
            Dim cmd As New SqlCommand(sSQL, connection)
            connection.Open()
            Dim datareader As SqlDataReader = cmd.ExecuteReader

            Me.Category1.Items.Clear()
            While datareader.Read
                If Not datareader("Segment 1 Desc").Equals(DBNull.Value) Then
                    Me.Category1.Items.Add(datareader("Segment 1 Desc"))
                End If
            End While
            Me.Category1.SelectedIndex = -1
            Me.Category1.Text = ""
            connection.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
You can add a "Please Select..." item at the top of the list. I think DropDownList does not support selectedindex = -1
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.