Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void bindgrid()
    {
string strConnString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(strConnString);
        DataTable dtab = new DataTable();

        string query = "SELECT [FirstName],[LastName],[Address],[City],[Country],[Age],[Sex],[Status],[Nationality],[Occupation],[EmailAddress],[Password],[ImageFilename] FROM [ProfileTbl] Where LastName='"+txtSearch.Text+"'";
        
        
        SqlDataAdapter sqlda = new SqlDataAdapter(query, conn);
        
        conn.Open();
        sqlda.Fill(dtab);
        
        if (dtab.Rows.Count > 0)
        {
           
            GridView1.DataSource = dtab;
            GridView1.DataBind();
           
        }
        conn.Close();

    protected void btnSearch_Click(object sender, EventArgs e)
    {
                bindgrid();
    }

When I query the LastName (for example:Smith)from database..All Smith in database record will appear or display to the gridview..My question is, if i search "Jones" which is not in the database record, I just want to prompt a message which is "Record not found"..how to validate that statements? is it "if else condition?"..

I am open to your all idea.Give me an idea for this..
Posted
Updated 12-May-13 9:17am
v2
Comments
[no name] 12-May-13 15:23pm    
You already are... do you suppose that if "dtab.Rows.Count > 0" tells you that you have data coming back from the database, "dtab.Rows.Count == 0" might tell you that there is no data coming back and you should show your error message? Or even more simply,
"if (dtab.Rows.Count > 0)
{ show the data }
else
{ show no data message }"
brighteyes24 12-May-13 15:46pm    
yah..you got it..thank you phantom
Maciej Los 12-May-13 16:39pm    
Please, post it as an answer.

Hey

If u used the GridView then u can also use templatefield and edit EmptyDataTemplate like

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
          <Columns>
              <asp:TemplateField HeaderText="Name">
                  <ItemTemplate>
                      <asp:Label ID="Label1" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
                  </ItemTemplate>
              </asp:TemplateField>
          </Columns>
          <EmptyDataTemplate>
              <asp:Label ID="Label2" runat="server" Text="No Data Found"></asp:Label>
          </EmptyDataTemplate>
      </asp:GridView>




Note :: In this code "Eval("Name")" Name is the column name which i was given in SQL
 
Share this answer
 
v4
use the condition
C#
if (GridView1.Rows.Count <= 0)

eg->
C#
if (GridView1.Rows.Count <= 0)
        {
            return "Record not found";
        }
 
Share this answer
 
First put scriptmanager on your aspx page

if (dtab.Rows.Count > 0)
{

GridView1.DataSource = dtab;
GridView1.DataBind();

}
else

{
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Record not Found');", true);
}
 
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