Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 12-May-13 10:06am
Edited 12-May-13 10:17am
v2
Comments
ThePhantomUpvoter at 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 at 12-May-13 15:46pm
   
yah..you got it..thank you phantom
Maciej Los at 12-May-13 16:39pm
   
Please, post it as an answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hey
 
If u used the GridView then u can also use templatefield and edit EmptyDataTemplate like
 
<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
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use the condition
if (GridView1.Rows.Count <= 0)
eg->
if (GridView1.Rows.Count <= 0)
        {
            return "Record not found";
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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);
}
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 BillWoodruff 230
4 Mathew Soji 225
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 14 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100