We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Populate DropDownList User Controls dynamically in VB.NET 3.5

Medium Priority
743 Views
Last Modified: 2012-05-11
I am new to asp.net vb and OO programming.  I would like to write a sub routine that populates a drop down list dynamically.  Specifically, I would like to be able to pass the reference to the drop down list user control object and then use that reference to add item values to the drop down list object.

See my code below:

Protected Sub Populate_DDL(ByRef sDropDwnObj As Object, ByVal sCodeValue As String, ByVal sLiteral As String)
        Try
            'Populate Supervisor Drop Down List
            Dim sSQL As String
            Dim dr As OracleDataReader

            sSQL = "select lmsdescription, lmsvalueid from lw.lw_lms_translatedvalues " & _
                   " where lmstypeid = '" & sCodeValue & "' " & _
                   " order by 1"

            'Execute SQL Statement
            Dim dbCon As New DBAccess(Master.GetEnvironment)

            dr = dbCon.ExecuteReader(sSQL)

            If dr.HasRows Then
                sDropDwnObj.Items.Clear()
                sDropDwnObj.Items.Add(New ListItem("--Please Select " & sLiteral & "-- ", ""))
                sDropDwnObj.Items.Add(New ListItem(dr(1), dr(2)))
            Else
                sDropDwnObj.Items.Clear()
                sDropDwnObj.Items.Add(New ListItem("--No " & sLiteral & "s Found-- ", ""))
            End If
            dbCon.Dispose()

        Catch ex As Exception
            Me.lblError.Text = Me.lblError.Text & " Populate_DDL:" & ex.Message.ToString() & "</br>"
        End Try
    End Sub

Comment
Watch Question

Full Stack .NET Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I tried your code and I am getting this error:

  Populate_DDL:Invalid column index specified

at this line:

  sDropDwnObj.Items.Add(New ListItem(dr(1), dr(2)))
Carlos VillegasFull Stack .NET Developer

Commented:
Sorry, try:
sDropDwnObj.Items.Add(New ListItem(dr(0), dr(1)))

Author

Commented:
Ah, that makes sense.  Not sure how I had that specified like that in my original code.  Anyway, your solution works perfectly now.

Thanks for your help.
Carlos VillegasFull Stack .NET Developer

Commented:
Glad to help buddy!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*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.