Populate DropDownList User Controls dynamically in VB.NET 3.5

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

jchaplwAsked:
Who is Participating?
 
Carlos VillegasConnect With a Mentor Full Stack .NET DeveloperCommented:
Hello, I did some changes to your code, try it:
    Protected Sub Populate_DDL(ByRef sDropDwnObj As ListControl, ByVal sCodeValue As String, ByVal sLiteral As String)
        Try
            'Populate Supervisor Drop Down List
            Dim sSQL As String
            Dim dr As System.Data.IDataReader

            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)

            sDropDwnObj.Items.Clear()
            
            While dr.Read()
                If sDropDwnObj.Items.Count = 0 Then
                    sDropDwnObj.Items.Add(New ListItem("--Please Select " & sLiteral & "-- ", ""))
                End If
                sDropDwnObj.Items.Add(New ListItem(dr(1), dr(2)))
            End While
            
            dr.Close()

            If sDropDwnObj.Items.Count = 0 Then
                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

Open in new window

0
 
jchaplwAuthor 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)))
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Sorry, try:
sDropDwnObj.Items.Add(New ListItem(dr(0), dr(1)))
0
 
jchaplwAuthor 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.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Glad to help buddy!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.