Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Here my problem is i want to check grdview null or not... i am using store procedure for check some condition

MY SP:

SQL
ALTER proc [Abileshop].[GrdOpenClose]
(
@compliantid varchar(50),
@out varchar(100) output
)
as
begin
declare @s varchar(10)
select @s=status from newcomp1 where compliantid=@compliantid order by id
if(@s='closed')
set @out='No Compliant Found'
else
begin
    select id,compliantid,priorty,status,convert(varchar,compdate,103)'CompliantDate' from newcomp1 where compliantid=@compliantid
end
end



when it return output i want to show in gridview or i want to load the data in gridview

MY CS:

C#
con = new SqlConnection(constr);
con.Open();
query = "GrdOpenClose";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@compliantid", txtcompid.Text);
cmd.Parameters.Add("@out", SqlDbType.VarChar, 100).Direction = ParameterDirection.Output;
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();
if (dt.Rows.Count==0)
{
    grdpending.DataSource = ds;//.Tables["newcomp1"];
    grdpending.DataBind();
}
else
{
    grdpending.EmptyDataText = "No Data Found";
}


How can i do that all the stuff...

Thanks...
Posted
v2
Comments
DamithSL 17-May-14 2:17am    
try below
if (ds.Tables.count>0 && ds.Tables[0].Rows.Count>0)
{
grdpending.DataSource = ds.Tables[0];
grdpending.DataBind();
}
else
{
grdpending.EmptyDataText = "No Data Found";
}
prasanna.raj 17-May-14 2:56am    
it showing error
"table collection could not be find"
DamithSL 17-May-14 3:20am    
check my answer

Please replace the following code

C#
da.Fill(ds);
DataTable dt = new DataTable();
if (dt.Rows.Count==0)
{
    grdpending.DataSource = ds;//.Tables["newcomp1"];
    grdpending.DataBind();
}
else
{
    grdpending.EmptyDataText = "No Data Found";
}


with

C#
da.Fill(ds);

// No need to keep Empty Data Text in Else condition because it appears only when gridview doesn't have any row. But we have to bind the null value with girdview

grdpending.EmptyDataText = "No Data Found";

grdpending.DataSource = null;

//Using two conditions here to avoid the null exception that occur when there is no table returned from stored procedure.
if (ds.Tables.Count>0)
{
 if(ds.Tables[0].Rows.Count>0)
 {
    grdpending.DataSource = ds;  //.Tables["newcomp1"];
 }
}

grdpending.DataBind();


I hope this will help you :)
 
Share this answer
 
Comments
prasanna.raj 17-May-14 5:03am    
nice..
C#
using(SqlConnection con = new SqlConnection(constr))
using (SqlCommand cmd = new SqlCommand("GrdOpenClose", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@compliantid", txtcompid.Text);
    SqlParameter outParam = cmd.Parameters.Add("@out", SqlDbType.VarChar, 100);
    outParam.Direction = ParameterDirection.Output;
    con.Open();
    DataTable dt = new DataTable();
    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
    {
        da.Fill(dt);
        string outmsg =string.Empty;
        if (outParam.Value != DBNull.Value)
        {
            outmsg =outParam.Value.ToString();
        }
        if (string.IsNullOrEmpty(outmsg))
        {
            if (dt!=null && dt.Rows.Count>0)
            {
                grdpending.DataSource = dt;
                grdpending.DataBind();
            }
        }
    }
}
 
Share this answer
 
v4
Comments
The condition is wrong DamithSL. If count is 0, then how it will bind? It should be the opposite.
DamithSL 17-May-14 2:28am    
there is a bug in codeproject when posting answers, i could not see the answer which i post. now only i able to see. i have added comment with the answer. please check <br>
http://www.codeproject.com/suggestions.aspx?msg=4823036#xx4823036xx[^]
It's ok now. :)
prasanna.raj 17-May-14 4:02am    
thanks... i think small Correction <br>
 <br>
if (outParam.Value != DBNull.Value)<br>
{<br>
<br>
grdpending.EmptyDataText = outParam.Value.ToString();<br>
grdpending.DataSource = dt;<br>
grdpending.DataBind();<br>
}<br>
if (string.IsNullOrEmpty(outmsg))<br>
{<br>
if (dt != null && dt.Rows.Count > 0)<br>
{<br>
grdpending.DataSource = dt;<br>
grdpending.DataBind();<br>
}<br>
}
You are binding the Database results to a DataSet, but trying to compare the count from a DataTable which is empty.

Do like below...
C#
ds = new DataSet();
da.Fill(ds);
DataTable dt = new DataTable();

If(ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
{
    grdpending.DataSource = ds.Tables[0];
    grdpending.DataBind();
}
else
{
    grdpending.EmptyDataText = "No Data Found";
}
 
Share this answer
 
Comments
prasanna.raj 17-May-14 2:38am    
wait i am trying...
prasanna.raj 17-May-14 2:45am    
all stuff skiping in else part only
prasanna.raj 17-May-14 2:54am    
it showing error "could not find table[0] " when i try to change table["tbname"] it always execute else part only...

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