Reputation: 37
I searched for above query and got so many answers but none of that answer is helpful for me. Hence Posting this question.
I have a gridview with Two dates.
When I select two dates and if data between two dates is available in database then it will get displayed on gridview. If data is not available then I want to show a Message on Label
i.e. "No Records Found"
Here is my code.
myConn.Open();
SqlCommand cmd = new SqlCommand(@"select User_id , LoginDate from LoginLog where LoginDate between ('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (GridView1.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
Upvotes: 1
Views: 4411
Reputation: 929
Try this method
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
private void Retrieve()
{
if(loadPositions() != null){
dgvEmployeePositions.DataSource = loadPositions();
}
else{
Lab4.Text = "No Records Found ";
}
}
private DataTable loadPositions()
{
DataTable dt = new DataTable();;
myConn.Open();
String q = "your query here";
MySqlCommand cmd = new MySqlCommand(q, connectionString);
MySqlDataReader r = cmd.ExecuteReader();
if(r.hasRows){
dt.Load(r);
return dt;
}
else{
return null;
}
}
Upvotes: 0
Reputation: 11
Add this to your ASPX file :
<asp:GridView ID="GridView1" runat="server" CellPadding="5" BorderStyle="Ridge" ShowHeaderWhenEmpty="true" EmptyDataText="No Records Found." EmptyDataRowStyle-ForeColor="Red">
And your CS will be :
myConn.Open();
SqlCommand cmd = new SqlCommand(@"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
myConn.Close();
Or you can see this link: Show asp.net Gridview Header when no data/records found
Upvotes: 1
Reputation: 929
Just check whether DataTable contains values or not.
myConn.Open();
SqlCommand cmd = new SqlCommand(@"select User_id , LoginDate from LoginLog where LoginDate between
('" + TextBox1.Text + "') and ('" + TextBox2.Text + "')", myConn);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
GridView1.DataSource = dt;
if (dt.Rows.Count > 0)
{
GridView1.DataBind();
}
else
{
Lab4.Text = "No Records Found ";
}
myConn.Close();
hope this helps. Regards :)
Upvotes: 0
Reputation: 381
Add this in gridview control
EmptyDataText="No Records Found"
Upvotes: 0