displaying msg when no records are found

Hi experts,

if no records are shown, how do i display "no result"

<div>
    <p>Show all employees with the following title:
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Selected="True">Sales Representative</asp:ListItem>
            <asp:ListItem>Sales Manager</asp:ListItem>
            <asp:ListItem>Vice President, Sales</asp:ListItem>
        </asp:DropDownList></p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
       
        <asp:SqlDataSource  id="SqlDataSource1"  runat="server"  ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Title] FROM [Employees] WHERE ([LastName] LIKE '%' + @LastName + '%')"
  FilterExpression="Title='{0}' OR FirstName LIKE '%{1}%'">
  <FilterParameters>
    <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
    <asp:ControlParameter Name="Name" ControlId="TextBox1" PropertyName="Text"/>
  </FilterParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="LastName" PropertyName="Text" Type="String" />
            </SelectParameters>
</asp:SqlDataSource>
       
       
       
            <p><asp:GridView
                id="GridView1"
                runat="server"
                DataSourceID="SqlDataSource1"
                AutoGenerateColumns="False">
                <columns>
                    <asp:BoundField Visible="False" DataField="EmployeeID" />
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                </columns>
            </asp:GridView>
           </div>

thansk
SirReadAlotAsked:
Who is Participating?
 
fredmastroConnect With a Mentor Commented:
Ok yeah and don't put it in Page_Load like I have it.. put it in GridView1_DataBound.....,  I also had changed AutoEventWrireUp to true.

    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
        If GridView1.Rows.Count = 0 Then
            '_mRowsFound = False
            DataMessageLabel.Text = "No Rows Found"
        Else
            '_mRowsFound = True
            DataMessageLabel.Text = ""
        End If
    End Sub



<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
 
    <p>Show all employees with the following title:
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            <asp:ListItem Selected="True">Sales Representative</asp:ListItem>
            <asp:ListItem>Sales Manager</asp:ListItem>
            <asp:ListItem>Vice President, Sales</asp:ListItem>
        </asp:DropDownList></p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
        <asp:SqlDataSource  id="SqlDataSource1"  runat="server"  ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  SelectCommand="SELECT [EmployeeID], [FirstName], [LastName], [Title] FROM [Employees] WHERE ([LastName] LIKE '%' + @LastName + '%')" 
  FilterExpression="Title='{0}' OR FirstName LIKE '%{1}%'">
  <FilterParameters>
    <asp:ControlParameter Name="Title" ControlId="DropDownList1" PropertyName="SelectedValue"/>
    <asp:ControlParameter Name="Name" ControlId="TextBox1" PropertyName="Text"/>
  </FilterParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="LastName" PropertyName="Text" Type="String" />
            </SelectParameters>
</asp:SqlDataSource>
      
            
            <p><asp:Label ID="DataMessageLabel" runat="server" /><br /><asp:GridView
                id="GridView1"
                runat="server"
                DataSourceID="SqlDataSource1"
                AutoGenerateColumns="False">
                <columns>
                    <asp:BoundField Visible="False" DataField="EmployeeID" />
                    <asp:BoundField HeaderText="First Name" DataField="FirstName" />
                    <asp:BoundField HeaderText="Last Name" DataField="LastName" />
                </columns>
            </asp:GridView>
     
    </form>
</body>
</html>
 
 
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
        If GridView1.Rows.Count = 0 Then
            DataMessageLabel.Text = "No Rows Found"
        Else
            DataMessageLabel.Text = ""
        End If
    End Sub
End Class

Open in new window

0
 
fredmastroCommented:
If I was to do this, I wouldn't use the built in datasource, might want to create your own method that pulls the information, puts it into a dataset, if there are no rows then you could set the dropdown list to say "No Records Found".  You could use instead of the SqlDataSource, use the ObectDataSource instead.  
0
 
SirReadAlotAuthor Commented:
hi,

i am filling  a gridview control, if no data is returned i need to display results
0
 
fredmastroCommented:
You could do this...
Add this to your front-end page, above your gridview...

<asp:Label ID="DataMessageLabel" runat="server" /><br />

Then on your code behind you could do something like...

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If GridView1.Rows.Count = 0 Then
            DataMessageLabel.Text = "No Rows Found"
        Else
            DataMessageLabel.Text = ""
        End If

    End Sub
0
 
fredmastroCommented:
You could also instead use the EmptyDataTemplate in the GridView.
Then you could setup a template that will show when no records are present.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.