当我从Web应用程序执行此sql语句时,它提供了一些错误 [英] when i executing this sql statement from web application it is providing some error

查看:74
本文介绍了当我从Web应用程序执行此sql语句时,它提供了一些错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

select sum(amountpaid) from tbl_payments where uid=29


它越来越空

如何检查状况
我正在尝试这样


it is getting null

how to check the condition
i am trying like this

if(cmd.executescaler()==null)


但它正在生成

无效的强制dbnull不能强制转换为其他类型.


but it is generating the

invalid cast dbnull cannot be cast to other types.

SqlCommand cmd = new SqlCommand("select sum(isnull(amountpaid,0)) from tbl_payments tbl_payments where uid=" + Label2.Text, con);
                amount = Convert.ToInt64(cmd.ExecuteScalar());
                if(amount!=0)
                 {




当标量和函数恢复为null时,它会得到无法将dbnull转换为其他类型的错误




when the scalar sum funtion returing null.it is getting the error like cannot cast from dbnull to other types

推荐答案

,您可以像这样尝试
you can try like this
select isnull(sum(isnull(amountpaid,0)),0) from tbl_payments where uid=29



您可以将此cmd execute分配给任何整数变量,并检查像这样的条件.

Hi,
you can assign this cmd execute to any integer variable,and check that condition like this.

int result = Convert.ToInt32(cmd.ExecuteScalar());
if(result==0)
{

}




您可以遵循以下代码:
Hi,

You can following code:
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
string CmdString = "select sum(isnull(amountpaid,0)) from tbl_payments where uid=@UID";
SqlCommand cmd = new SqlCommand(CmdString, con);
cmd.Parameters.AddWithValue("@UID", 5734999994);
con.Open();
double AmountPaid;
if (cmd.ExecuteScalar() != null)
{
    AmountPaid = Convert.ToDouble(cmd.ExecuteScalar());
}
con.Close();


这篇关于当我从Web应用程序执行此sql语句时,它提供了一些错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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