• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

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
0
Murray Brown
Asked:
Murray Brown
1 Solution
 
BlueYonderCommented:
Me.SelectedIndex = -1
0
 
CodeCruiserCommented:
That would be Me.Cat1.SelectedIndex = -1
0
 
Friman001Commented:
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
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now