Click here to Skip to main content
15,912,317 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m going to bind the gridview in c#
when it comese to this
C#
if (ds.Tables.Count > 0)
                {
                    if (ds != null && ds.Tables.Count > 0)
                    ///if (ds.Tables[0].Rows.Count > 0)
                    {
                        gdvNotice.DataSource = ds.Tables[0];
                        Session["Get_Notice"] = ds.Tables[0];
                        gdvNotice.DataBind();

                    }
                    else
                    {
                        gdvNotice.DataSource = null;
                        gdvNotice.DataBind();
                    }
                }


Here returns 1 and
C#
if (ds.Tables.Count > 0)

in second if its false and go out of the block
C#
if (ds.Tables[0].Rows.Count > 0)


What I have tried:

if (ds.Tables.Count > 0)
{

if (ds.Tables[0].Rows.Count > 0)
{
gdvNotice.DataSource = ds.Tables[0];
Session["Get_Notice"] = ds.Tables[0];
gdvNotice.DataBind();

}
else
{
gdvNotice.DataSource = null;
gdvNotice.DataBind();
}
}
Posted
Updated 7-Sep-16 4:12am
Comments
ZurdoDev 7-Sep-16 9:53am    
If there are no rows then either your sql is not running or your sql is returning now rows. See what is happening in sql.
faisal23 7-Sep-16 10:00am    
hello,
in first if case it return 1 and second if case condition is false and throw out the block
ZurdoDev 7-Sep-16 10:01am    
I do not understand what you are asking.
faisal23 7-Sep-16 10:04am    
plz see my code in first if block means
if (ds.Tables.Count > 0)
it return true means 1
in second if block means
if (ds.Tables[0].Rows.Count > 0)
it become false and throw out
ZurdoDev 7-Sep-16 10:05am    
I already explained that. You do not have any rows.

1 solution

As discussed in comments, your sql table may have records in it but they are not getting in C# so make sure you are calling your sql properly and passing in all the correct parameters. You can use Sql Profiler to trace and see what exactly is being sent to SQl.

Also, when binding your gridview, you likely do not want to check for rows. Just bind it to the DataTable and be done. If there are no rows, your grid will be empty. If there are, there will be rows. Don't worry about checking row count to determine how to databind.
 
Share this answer
 
Comments
faisal23 7-Sep-16 10:26am    
can you please tell me code how not to check rows and directly bind to data table in my code??
ZurdoDev 7-Sep-16 10:28am    
Just remove the if statement where you are checking the row count.

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