Need to check for empty drop down and hide if empty

Hi,

I am loading a drop down with values from a table, but I need to check to see if it is empty and if so I need to hide it. I'm not sure how to go about doing that. Any help would be greatly appreciated. This is the code I have for the drop down:
Private Sub LoadAllCourses(ByVal sql As String)
        Dim Myconn As SqlConnection
        Dim Mycomm As SqlCommand
        Dim dtr As SqlDataReader
        Dim result
        'Dim sql As String
        Myconn = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("gDataSource"))
        Myconn.Open()
        Mycomm = New SqlCommand(sql, Myconn)
        dtr = Mycomm.ExecuteReader
        ddCourses.DataSource = dtr
        ddCourses.DataTextField = "CourseID"
        ddCourses.DataValueField = "CourseID"
        ddCourses.DataBind()
        ddCourses.Items.Insert(0, New ListItem("Select One", -1))
        dtr.Close()
        Myconn.Close()
    End Sub
 
This is the sub that is calling the drop down:
 
Private Sub BuildSearch()
        Dim Myconn As SqlConnection
        Dim Mycomm As SqlCommand
        Dim dtr As SqlDataReader
        Dim result, vIdArray, i
        Dim sql, vCourse, CourseID
        Myconn = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("gDataSource"))
        Myconn.Open()
        sql = "SELECT * FROM SA_Reports_Users Where username = '" & Session("gUserName") & "' AND semester = '" & Session("semester") & "' ORDER BY Course"
        'Response.Write(sql)
        'Response.End()
        Mycomm = New SqlCommand(sql, Myconn)
        Try
            dtr = Mycomm.ExecuteReader()
            If dtr.HasRows Then
                Session("sql") = "SELECT DISTINCT CourseID FROM SA_EVALUATION_COMPLETED WHERE 1=2"
                While dtr.Read()
                    Dim Course As String = dtr("Course")
                    If Course <> "All" Then
                        Course = dtr("semester") & "." & dtr("Course")
                    End If
                    'If Course = "All" Then Course = ""
                    'Session("sql") += String.Format(" OR CourseID LIKE '%' + '{0}' + '%'", Course)
                    If Course = "All" Then Course = dtr("semester")
                    Session("sql") += String.Format(" OR CourseID LIKE '%' + '{0}' + '%'", Course)
                End While
                Session("sql") += " ORDER BY CourseID"
            End If
        Catch
        End Try
        dtr.Close()
        Myconn.Close()
        Response.Write(Session("sql"))
        Response.End()
        If (Session("sql")) = "" Then
        Else
            LoadAllCourses(Session("sql"))
            btnReport.Visible = True
        End If
    End Sub

Open in new window

savache27Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prairiedogCommented:
What do you want to check? The datatable?
What do you want to hide? The DropDownList?
0
omegaomegaDeveloperCommented:
Hello, savache27,

How about adding:

     ddCourses.Visible = False

between lines 56 and 57 of your snippet?

Hmmm...  That seems pretty simple.  Perhaps it's something else that you're asking, and I've not understood the question...

Cheers,
Randy
0
savache27Author Commented:
Hi, I want to check to see if the DropDownList is empty. Like if LoadAllCourses() produces no results I don't want it to show the ddCourses dropdownlist.
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

prairiedogCommented:
Something like this?
dtr = Mycomm.ExecuteReader
If (dtr.HasRow) Then
	ddCourses.Visible = True
	ddCourses.DataSource = dtr
	ddCourses.DataTextField = "CourseID"
	ddCourses.DataValueField = "CourseID"
	ddCourses.DataBind()
	ddCourses.Items.Insert(0, New ListItem("Select One", -1))
Else
	ddCourses.Visible = False
End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prairiedogCommented:
Sorry, it should be dtr.HasRows, not dtr.HasRow.
0
savache27Author Commented:
prairiedog, that works perfectly! Thank you so much for your help!!! I really appreciate your time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.