SqlDataReader: How do I hide/show panel if NO RECORD exists in the database?

Posted on 2006-04-19
Last Modified: 2008-03-17
How do I hide/show panel if NO RECORD exists in the database?


<script runat="server">
    Protected Sub btnCheckAccount_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlCommand
        Dim MyReader As SqlDataReader
        Dim IDParam As SqlParameter
        MyConnection = New SqlConnection(ConfigurationManager.AppSettings("MM_CONNECTION_STRING_APDTMain"))
        MyCommand = New SqlCommand()
        MyCommand.CommandText = "SELECT * from vw_MemberStatusCheck WHERE Account = @ID"
        MyCommand.CommandType = CommandType.Text
        MyCommand.Connection = MyConnection
        IDParam = New SqlParameter()
        IDParam.ParameterName = "@ID"
        IDParam.Value = txtAccount.Text
        MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
        While MyReader.Read
            txtDateExpire.Text = MyReader("DateExpire").ToString()
            txtFirstName.Text = MyReader("FirstName")
            txtMemberType.Text = MyReader("MemberType")
            cbxActive.Checked = MyReader("Active")
            txtAccount.Text = MyReader("Account")
        End While
        If Not MyReader.Read Then
            pnlNoRecords.Visible = True
            pnlActive.Visible = False
            pnlExpired.Visible = False
        End If
        If cbxActive.Checked = True Then
            pnlActive.Visible = True
            pnlNoRecords.Visible = False

        End If
        If cbxActive.Checked = False Then
            pnlExpired.Visible = True
            pnlNoRecords.Visible = False
            pnlActive.Visible = False
        End If
    End Sub

<asp:Panel ID="pnlExpired" runat="server" Visible="false">

    <asp:Literal ID="txtFirstName" runat="server"></asp:Literal>, it seems
    that your membership with APDT expired on <asp:Literal ID="txtDateExpire" runat="server"></asp:Literal>.&nbsp; Renew now &gt;</p>
    <asp:Panel ID="pnlActive" runat="server" Visible="false">
<p> You are an active: <asp:Literal ID="txtMemberType" runat="server"></asp:Literal>.</p>
 <asp:CheckBox ID="cbxActive" runat="server" Visible="false"/>
    <asp:Panel ID="pnlNoRecords" runat="server" Visible="false">
<p> No Records Found.  Please enter a valid Member Number.</p>
Question by:jumpseatnews
    LVL 15

    Accepted Solution

    Just add in

    pnlNoRecords.Visible = True
    pnlActive.Visible = False
    pnlExpired.Visible = False

    While DataReader.Read()
         'You have records so show the right panel and carry on.
    End While

    Author Comment

    That did the trick!  Thanks GavinMannion.


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now