如何检查Gridview Null与否 [英] How to check Gridview Null or not

查看:72
本文介绍了如何检查Gridview Null与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里我的问题是我想检查grdview null是否...我正在使用商店程序检查某些条件



MY SP:



Hi,
Here my problem is i want to check grdview null or not... i am using store procedure for check some condition

MY SP:

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







W如果它返回输出我想在gridview中显示或者我想在gridview中加载数据



MY CS:






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

MY CS:

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...

推荐答案

请替换以下代码



Please replace the following code

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





with

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 :)


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


您将数据库结果绑定到 DataSet ,但是试图将 DataTable 中的计数进行比较,这是空的。



如下所示...

You are binding the Database results to a DataSet, but trying to compare the count from a DataTable which is empty.

Do like below...
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";
}


这篇关于如何检查Gridview Null与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆