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

ASP.NET VB DataList Control - Show message when record set is empty

I have a simple ASP.NET VB page that pulls records from a database and displays the results nicely on the page. What I would like to do however is to have a message "No Records Found" displayed if there are no records. If there are records, I would like the DataList to be displayed.

This is very easy in Classic ASP, but I cant figure it in MS. .NET.
0
guildernet
Asked:
guildernet
  • 4
  • 2
1 Solution
 
guildernetAuthor Commented:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" %>

<script runat="server">

    Protected Sub ListView1_SelectedIndexChanged(sender As Object, e As System.EventArgs)

    End Sub
    
    Protected Sub TennesseeListView_SelectedIndexChanged(sender As Object, e As System.EventArgs)

    End Sub

    Protected Sub Page_Load(sender As Object, e As System.EventArgs)

    End Sub

    Protected Sub RsNisky_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
   
        
        If DataList4.Items.Count = 0 Then
            'There are no records
            DataList4.Visible = False
            lblnisky.Visible = True
        Else
            DataList4.Visible = True
            lblnisky.Visible = False
        End If
        
    End Sub

    Protected Sub DataList4_SelectedIndexChanged(sender As Object, e As System.EventArgs)

       
        
    End Sub

    Protected Sub DataList3_SelectedIndexChanged(sender As Object, e As System.EventArgs)

    End Sub

    Protected Sub RsRotterdam_Selecting(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)
        
        
        
    End Sub
</script>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   

   <table cellspacing="0" cellpadding="0" border="0" align="center" width="750">
<tr>
	<td>
	 <p align="center" class="style1">Employee Emergency Information Center</p>

     <div class="SubTitleTxt">
       <p class="Topboxtxt">Welcome to the SI Group Employee Emergency Information Center. Currently this site covers our New York, Texas and Tenessee Operations.</p>
    
    
     <%--  *************** Freeport Texas Events ********************** --%>
       
       <asp:SqlDataSource 
             ID="TexasAlerts" runat="server" 
             ConnectionString="<%$ ConnectionStrings:TexasAlerts %>" 
             
             
             SelectCommand="SELECT [ArticleTitle], [DatePosted], [ArticleID], [EventTypeTxt], [EventTypeImage] FROM [sqryEEICAdminView] WHERE ([EEICLocationID] = @EEICLocationID)">
         <SelectParameters>
             <asp:Parameter DefaultValue="4" Name="EEICLocationID" Type="Int32" />
         </SelectParameters>
         </asp:SqlDataSource>
        </div>
       
       
       <%--  *************** Freeport Texas DataList ********************** --%>

          <br />
        
        <div align="center">
        
        <asp:DataList ID="DataList1" runat="server" CellPadding="4" 
            DataKeyField="ArticleID" DataSourceID="TexasAlerts" ForeColor="#333333" 
            ShowFooter="False">
            <AlternatingItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#184cb1" Font-Bold="True" Font-Italic="False" 
                Font-Names="Verdana" Font-Overline="False" Font-Size="Small" 
                Font-Strikeout="False" Font-Underline="False" ForeColor="White" 
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BackColor="#E7E9E4" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />


               <HeaderTemplate>
                    Alerts for Freeport Texas 
               </HeaderTemplate>
             

            <ItemTemplate>
               

               <img alt="Image" align="middle" src="<%# Eval("EventTypeImage") %>">&nbsp;&nbsp;

               <span class="style4">** <%# Eval("EventTypeTxt") %> **</span> &nbsp;

               <span class="style3">&nbsp;<a href="event.aspx?Article=<%# Eval("ArticleID") %>"><%# Eval("ArticleTitle") %></a></span>&nbsp;&nbsp;<span class="style3"><b>Posted</b> <%# Eval("DatePosted") %></span>


            </ItemTemplate>
            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                ForeColor="#333333" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:DataList>

        <br />

        </div> 

      
        <!-- **************** END TEXAS EVENTS ************************** -->





      <!-- **************** TENNESSEE EVENTS ************************** -->
       

       <div align="center">

	    <asp:SqlDataSource ID="RsTennessee" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TexasAlerts %>" 
            
            
            SelectCommand="SELECT [ArticleTitle], [DatePosted], [ArticleID], [EventTypeTxt], [EventTypeImage] FROM [sqryEEICAdminView] WHERE ([EEICLocationID] = @EEICLocationID)">
            <SelectParameters>
                <asp:Parameter DefaultValue="5" Name="EEICLocationID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

        <br />
        <asp:DataList ID="DataList2" runat="server" CellPadding="4" 
            DataKeyField="ArticleID" DataSourceID="RsTennessee" ForeColor="#333333" 
            ShowFooter="False">
            <AlternatingItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />
            <EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
                Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left" 
                VerticalAlign="Middle" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#184CB1" Font-Bold="True" Font-Italic="False" 
                Font-Names="Verdana" Font-Overline="False" Font-Size="Small" 
                Font-Strikeout="False" Font-Underline="False" ForeColor="White" 
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BackColor="#E7E9E4" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />


                <HeaderTemplate>
                    Alerts for Newport Tennessee 
               </HeaderTemplate>


            <ItemTemplate>
               

               <img alt="Image" align="middle" src="<%# Eval("EventTypeImage") %>">&nbsp;&nbsp;

               <span class="style4">** <%# Eval("EventTypeTxt") %> **</span> &nbsp;

               <span class="style3">&nbsp;<a href="event.aspx?Article=<%# Eval("ArticleID") %>"><%# Eval("ArticleTitle") %></a></span>&nbsp;&nbsp;<span class="style3"><b>Posted</b><%# Eval("DatePosted") %></span>


            </ItemTemplate>


            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                ForeColor="#333333" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:DataList>

        <br />


           <!-- **************** ROTTERDAM EVENTS ************************** -->

           <asp:SqlDataSource ID="RsRotterdam" runat="server" 
               ConnectionString="<%$ ConnectionStrings:TexasAlerts %>" 
               
               SelectCommand="SELECT [ArticleTitle], [DatePosted], [ArticleID], [EventTypeImage], [EventTypeTxt] FROM [sqryEEICAdminView] WHERE ([EEICLocationID] = @EEICLocationID)" 
               onselecting="RsRotterdam_Selecting">
               <SelectParameters>
                   <asp:Parameter DefaultValue="3" Name="EEICLocationID" Type="Int32" />
               </SelectParameters>
           </asp:SqlDataSource>
           <br />
        <asp:DataList ID="DataList3" runat="server" CellPadding="4" 
            DataKeyField="ArticleID" DataSourceID="RsRotterdam" ForeColor="#333333" 
            ShowFooter="False" onselectedindexchanged="DataList3_SelectedIndexChanged">
            <AlternatingItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />
            <EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
                Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left" 
                VerticalAlign="Middle" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#184CB1" Font-Bold="True" Font-Italic="False" 
                Font-Names="Verdana" Font-Overline="False" Font-Size="Small" 
                Font-Strikeout="False" Font-Underline="False" ForeColor="White" 
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BackColor="#E7E9E4" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />


                <HeaderTemplate>
                    Alerts for Rotterdam Junction 
               </HeaderTemplate>


            <ItemTemplate>
               

               <img alt="Image" align="middle" src="<%# Eval("EventTypeImage") %>">&nbsp;&nbsp;

               <span class="style4">** <%# Eval("EventTypeTxt") %> **</span> &nbsp;

               <span class="style3">&nbsp;<a href="event.aspx?Article=<%# Eval("ArticleID") %>"><%# Eval("ArticleTitle") %></a></span>&nbsp;&nbsp;<span class="style3"><b>Posted</b> <%# Eval("DatePosted") %></span>


            </ItemTemplate>


            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                ForeColor="#333333" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:DataList>

           <br />

           <!-- **************** NISKAYUNA EVENTS ************************** -->

           <asp:SqlDataSource ID="RsNisky" runat="server" 
               ConnectionString="<%$ ConnectionStrings:TexasAlerts %>" 
               
               SelectCommand="SELECT [ArticleTitle], [DatePosted], [ArticleID], [EventTypeTxt], [EventTypeImage] FROM [sqryEEICAdminView] WHERE ([EEICLocationID] = @EEICLocationID)" 
               onselecting="RsNisky_Selecting">
               <SelectParameters>
                   <asp:Parameter DefaultValue="2" Name="EEICLocationID" Type="Int32" />
               </SelectParameters>
           </asp:SqlDataSource>
           <br />

         
        <asp:DataList ID="DataList4" runat="server" CellPadding="4" 
            DataKeyField="ArticleID" DataSourceID="RsNisky" ForeColor="#333333" 
            ShowFooter="False" onselectedindexchanged="DataList4_SelectedIndexChanged">
            <AlternatingItemStyle BackColor="White" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />
            <EditItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
                Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left" 
                VerticalAlign="Middle" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#184CB1" Font-Bold="True" Font-Italic="False" 
                Font-Names="Verdana" Font-Overline="False" Font-Size="Small" 
                Font-Strikeout="False" Font-Underline="False" ForeColor="White" 
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <ItemStyle BackColor="#E7E9E4" Font-Bold="False" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                HorizontalAlign="Left" VerticalAlign="Middle" />


                                      
                


                <HeaderTemplate>
                           Alerts for Niskayuna New York 
               </HeaderTemplate>


            <ItemTemplate>
               

               <img alt="Image" align="middle" src="<%# Eval("EventTypeImage") %>">&nbsp;&nbsp;

               <span class="style4">** <%# Eval("EventTypeTxt") %> **</span> &nbsp;

               <span class="style3">&nbsp;<a href="event.aspx?Article=<%# Eval("ArticleID") %>"><%# Eval("ArticleTitle") %></a></span>&nbsp;&nbsp;<span class="style3"><b>Posted</b> <%# Eval("DatePosted") %></span>


            </ItemTemplate>


            <SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True" Font-Italic="False" 
                Font-Overline="False" Font-Strikeout="False" Font-Underline="False" 
                ForeColor="#333333" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:DataList>

          
           <asp:Label ID="lblnisky" runat="server" 
               Text="** Currently there are no Alerts for Niskayuna, New York **" 
               Visible="False" CssClass="NoAlertTxt"></asp:Label>

        </div>
        <br />

        <!-- **************** END NISKAYUNA EVENTS ************************** -->


       <br /><br />

       	</td>
</tr>
</table>

</asp:Content>

Open in new window

0
 
käµfm³d 👽Commented:
What if you did something like:

Markup
<asp:DataList ID="DataList1" runat="server" CellPadding="4" 
              DataKeyField="ArticleID" DataSourceID="TexasAlerts" ForeColor="#333333" 
              ShowFooter="False">

...

    <FooterTemplate>
        No Data!
    </FooterTemplate>
</asp:DataList>

Open in new window


Code-Behind
...

Protected Sub RsNisky_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)

    If DataList4.Items.Count = 0 Then
        'There are no records
        DataList4.Visible = False
        lblnisky.Visible = True
        Me.DataList1.ShowFooter = True    ' Added
    Else
        DataList4.Visible = True
        lblnisky.Visible = False
    End If

End Sub

...

Open in new window

0
 
guildernetAuthor Commented:
No, no such luck i'm afraid.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
käµfm³d 👽Commented:
Have you tried using the Selected event rather than the Selectng event? Selecting occurs before data is retrieved; Selected occurs after.
0
 
guildernetAuthor Commented:
The solution actually ends up being the following.

Create a SQLData Source that connects to the table, or view that contains the data that you need.

Then, use the ListView Server control to return the data to the page. Once you have the correct data showing up on the page, I hand tweaked it to display in a table structure that I wanted. It works great.

In ListView Server control there is an option for what to display if there are no records to return. I put my custom CSS and Message in between those tags and it works like a charm.
0
 
guildernetAuthor Commented:
Ended up finding the answer though google.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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