Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a gridview in which i am displaying data from sql server.now on my UI i want to display a text "No Data Available" when there is no data to be shown in the grid view i.e. the table from which im displaying data in grid view is empty. the problem is how should i give the condition that gridview is empty?

i used this code but its not displaying the text in the label.

C#
SqlCommand cmd = new SqlCommand("select * from Requests", new SqlConnection(myConnectionString));

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);
        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                lblError.Text = "Dataset table contains data";
            }
        }

        else
        {
            lblError.Visible = true;
            lblError.Text = "Dataset does not contains data";
        }
Posted
v3

Hi
I made a small change in your condition. Please check this

C#
da.Fill(ds);
         if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
         {
             lblError.Text = "Dataset table contains data";
         }
         else
         {
             lblError.Visible = true;
             lblError.Text = "Dataset does not contains data";
         }
 
Share this answer
 
Comments
workoholic91 24-Nov-12 11:14am    
this worked!! thanks alot!!
You can do it in two ways.

1. EmptyDataText property.
2. DataSet or DataTable row counts.

Please check Asp.net show gridview header when there is no data or empty[^] for the clear example.
 
Share this answer
 
v2
Comments
workoholic91 24-Nov-12 10:58am    
i used the data set property but it didnt work.. or am i doing it wrong? i've uploaded the code.
Hi Sweettt,

Sorry for the late response. I was offline. I just saw you accepted the answer given by @Dominic Abraham, which is correct. But I just want to make you understand that mine is also correct, you just get it wrong. Let me explain...

As per your code, you are doing like if (ds.Tables.Count > 0), which will always be true, because DataSet will contain a table for sure, but it may be empty.

So, you need to check whether the table contains data or not. But you are checking whether DataSet contains table or not, which will not work.

That's why the answer given by @Dominic Abraham also checks that condition.

And if you can check the code which is there in the link I provided, it does the same thing only, nothing different from his answer. I think you have not checked properly.
The condition in that article is like...

if(ds.Tables[0].Rows.Count==0)
{
// Means table inside DataSet has no rows
// Empty data condition.
}

which is correct as it checks that table inside that DataSet contains data or not by checking the rowcount.

But you are doing with the DataSet like below...

if (ds.Tables.Count > 0)
{}

which checks whether DataSet contains table or not, which is always true, so else part will never be executed.

Hope you understand what I elaborated.
So, the answer given by me is correct.
Please reply with your comments.

Thanks.
Tadit
workoholic91 24-Nov-12 15:46pm    
i really missed out the link that u have provided.. didnt see it before.. n now that u have explained it completely i understnd this more clearly! thank u for that n sorry for missing the link
Hello Sweettt,

That is completely ok... No problem.
And the most important thing is, you have understood the concept.
I feel glad that I could explain somebody the concept and he/she gets it.
Thank a lot for accepting the answer...
Hope to meet you in some social sites, if you can add me, it would be nice. :)

Thanks and regards,
Tadit

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