Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Populate DropDownList User Controls dynamically in VB.NET 3.5

Posted on 2011-04-18
5
Medium Priority
?
690 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

0
Comment
Question by:jchaplw
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35420054
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
 

Author Comment

by:jchaplw
ID: 35424769
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
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35424811
Sorry, try:
sDropDwnObj.Items.Add(New ListItem(dr(0), dr(1)))
0
 

Author Comment

by:jchaplw
ID: 35424876
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
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35424891
Glad to help buddy!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question