Click here to Skip to main content
16,017,707 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to display "No Record Found" message when we search the data into the Database, if the record is found then related data shows in Grid View and if the Data is not found then display a "No Record Found" message ?
Posted
Updated 23-Sep-20 15:25pm
Comments
Graeme_Grant 21-Aug-17 8:00am    
What type of application is it? Console, Winform, Wpf, Xamarin, ASP.NET Webforms, ASP.NET MVC, ASP.NET CORE, etc...? Each does it differently!

Use EmptyDataTextproperty of gridview or use EmptyDataTemplate.

XML
<asp:gridview id="GridView1" runat="server" 
EmptyDataText="No Records found">

</asp:gridview>


or
C#
// if you want to style empty row
 <emptydatarowstyle backcolor="LightBlue" forecolor="Red" />
        <emptydatatemplate>
// you can insert anything image message or text message
 //  <asp:image id="NoDataImage" imageurl="~/images/Image.jpg" runat="server" xmlns:asp="#unknown" />
            No Data Found.  

        </emptydatatemplate> 
 
Share this answer
 
you have another option with datatable,

C#
DataTable dt = new DataTable();
            try
            {
                Con_Open();
                cmd = new SqlCommand("select * from Stu", con);
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    dt.Columns.Add(new DataColumn("Reg No"));
                    dt.Columns.Add(new DataColumn("Name"));
                    dt.Columns.Add(new DataColumn("Mark1"));
                    dt.Columns.Add(new DataColumn("Mark2"));
                    dt.Columns.Add(new DataColumn("Mark3"));
                    while (dr.Read())
                    {
                        DataRow temp;
                        temp = dt.NewRow();
                        temp[0] = (System.Convert.IsDBNull(dr[0]) ? "" : dr[0].ToString());
                        temp[1] = (System.Convert.IsDBNull(dr[1]) ? "" : dr[1].ToString());
                        temp[2] = (System.Convert.IsDBNull(dr[2]) ? "" : dr[2].ToString());
                        temp[3] = (System.Convert.IsDBNull(dr[3]) ? "" : dr[3].ToString());
                        temp[4] = (System.Convert.IsDBNull(dr[4]) ? "" : dr[4].ToString());
                        dt.Rows.Add(temp);
                    }
                    cmd.Dispose();
                    dr.Close();
                    con.Close();
                }
                else
                {

                    dt.Columns.Add(new DataColumn("Records"));
                    DataRow temp;
                    temp = dt.NewRow();
                    temp[0] = "No Records Found!!";
                    dt.Rows.Add(temp);
                }             
            }


regards
sarva
 
Share this answer
 
 
Share this answer
 
Gridview has property called EmptyDataText .. set it to whatever message you want to display...

ASP.NET
 <asp:gridview id="grdViewKeyType" runat="server" allowpaging="True" autogeneratecolumns="False" xmlns:asp="#unknown">
                                BackColor="White" BorderColor="#999999" BorderStyle="Solid" 
                    CellPadding="5" CellSpacing="1"
                                CssClass="Grid" 

EmptyDataText="No Records found" 

....

</asp:gridview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900