Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net load DropDownList without selecting one of the items

Hi

I am using the following code to to populate a DropDownList. How do I avoid the first item being automatically selected?

    Sub Fill_Category1_Combobox()
        Try

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

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

            Me.Cat1.Items.Clear()
            While datareader.Read
                If Not datareader("Segment 1 Desc").Equals(DBNull.Value) Then
                    Me.Cat1.Items.Add(datareader("Segment 1 Desc"))
                End If
            End While
            connection.Close()


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

    End Sub
Avatar of BlueYonder
BlueYonder

Me.SelectedIndex = -1
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello,

    In C#, I use RadControls.  When I do RadComboBoxWorkCenter.SelectedIndex = -1, it still selects the first item.  It also did this with the ASP:DropdownList.  What I had been doing before I saw this past was added a new item to the list.

    Using asp controls, I was able to select a blank value by doing this:
// C# Code.
this.DropDownList1.Items.Add(new ListItem());
this.DropDownList1.SelectedValue = "";

// VB Code.
Me.DropDownList1.Items.Add(New ListItem())
Me.DropDownList1.SelectedValue = ""

Open in new window


I only tested this in C#!

Ryan F
Avatar of Murray Brown

ASKER

Thanks