• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 196
  • Last Modified:

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

0
savache27
Asked:
savache27
  • 3
  • 2
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now