0
votes

I have a stored procedure in SQL that searches employee details. When it finds something,it returns and displays the data in a gridview. But how can I handle if it did not return anything? like when 'no record is found'?

using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBReader"].ConnectionString))
{
    using (SqlCommand com = new SqlCommand("[Reader].[usp_SearchUser]", con))
    {
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add("@empID", SqlDbType.Int).Value = this.EmpID;
        con.Open();
        int result = com.ExecuteNonQuery();
        if (result == 0)
        {
            this.NoRecord = "No Record Found";
        }
        else
        {
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            search.DataSource = ds;
            search.DataBind();
        }

    }
}
2
why don't you just check ds.Tables[0].Rows.Count == 0? - Roman Pekar
You have pretty much handled it in the code. What else do you want to do? If you need to display some message I think you can do it in the gridview based on the returned results. - Nilesh

2 Answers

0
votes

Did not get what is your exact question? do you want the gridview property when there is no data then it will show as no records found i.e. EmptyDataText="No records Found" e.g.

<asp:GridView ID="GridView1" runat="server" EmptyDataText="No records Found">
    <Columns>
          <asp:boundfield datafield="empID" headertext="Employee ID"/>
          <asp:boundfield datafield="empName" headertext="Employee Name"/>
    </Columns>
   </asp:GridView>
0
votes

I suppose you use a webcontrol GridView? So you might use the GridView.EmptyDataTemplate to have full control of what to render if no data has been bound.

<asp:gridview id="yourGridView" runat="server">
    <emptydatatemplate>
        No Data Found!
        <img src="noData.jpg"/>
    </emptydatatemplate> 
  </asp:gridview>

Or just use EmptyDataText Property if you just want to show a text Message

<asp:gridview id="yourGridView" emptydatatext="No Data Found" runat="server">
     ....
</asp:gridview>