对象不能从dbnull转换为数据表计算方法中的其他类型 [英] object cannot be cast from dbnull to other types in datatable compute method

查看:106
本文介绍了对象不能从dbnull转换为数据表计算方法中的其他类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,





int sum = Convert.ToInt32(dt.Compute(SUM(Amount),Section> 0));



我想使用聚合函数sum()从特定条件的数据表中获取总量,但上面的语句抛出错误对象无法转换从dbnull到其他类型

Hello,


int sum = Convert.ToInt32(dt.Compute("SUM(Amount)", "Section>0"));

I want to use aggregate function sum() to get total amount from datatable for specific conditions,but the above statement throws error object cannot be cast from dbnull to other types

推荐答案

试试这个:

Try this:
int sum = 0;
object calc = dt.Compute("SUM(ISNULL(Amount, 0))", "Section>0");
bool converted = Integer.TryParse(calc, sum);
if (!converted) // some message;
else
//work normally


这篇关于对象不能从dbnull转换为数据表计算方法中的其他类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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