We help IT Professionals succeed at work.

ASP.NET Combobox Data

Larry Brister
on
Medium Priority
225 Views
Last Modified: 2013-11-26
I'm populating a combobox with the code below.  How do I add the item " - Select All - " to the top of the list?

 Public Sub getZipCode()
        Dim objConn As SqlConnection
        objConn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
        Dim oCom2 As SqlCommand = New SqlCommand
        oCom2.Connection = objConn
        objConn.Open()
        Dim sSQL As String
        sSQL = "Select [zipcode] "
        sSQL = sSQL & "from tblMasterList where zipcode <>''"
        sSQL = sSQL & "group by [zipcode] "
        sSQL = sSQL & "order by [zipcode]"
        oCom2.CommandText = sSQL
        'Dim dR As SqlDataReader
        getZip.DataSource = oCom2.ExecuteReader()
        getZip.DataBind()
        oCom2.Dispose()
        objConn.Close()

    End Sub
Comment
Watch Question

it would be easier to use a dataadapter to get a table rather than use a cursor

Public Sub getZipCode()
        Dim objConn As SqlConnection
        objConn = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
        Dim oCom2 As SqlCommand = New SqlCommand
        oCom2.Connection = objConn
        objConn.Open()
        Dim sSQL As String
        sSQL = "Select [zipcode] "
        sSQL = sSQL & "from tblMasterList where zipcode <>''"
        sSQL = sSQL & "group by [zipcode] "
        sSQL = sSQL & "order by [zipcode]"
        oCom2.CommandText = sSQL
     
        dim DA as new SqlDataAdapter(oCom2)
        dim dt as new DataTable
        DA.Fill(dt)  '<--- put the results into dt

        'now add the top item
        DataRow dr = dt.NewRow()
        dr(0) = "- Select Item -"
        dt.Rows.InsertAt(dr, 0)

        getZip.DataSource = dt
        getZip.DataBind()
        oCom2.Dispose()
        objConn.Close()

    End Sub
Larry Bristersr. Developer

Author

Commented:
Solar Flare

I'm getting "Datarow is a type and cannot be used in an expression message."

This is an ascx page

DataRow(da = dt.NewRow())   -  Several errors here
        dr(0) = "- Select Item -"
        dt.Rows.InsertAt(dr, 0)
oh lol, that is C# type syntax!

        dim DA as new SqlDataAdapter(oCom2)
        dim dt as new DataTable
        DA.Fill(dt)  '<--- put the results into dt

        'now add the top item
        dim dr as DataRow = dt.NewRow()
        dr(0) = "- Select Item -"
        dt.Rows.InsertAt(dr, 0)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.