无法将对象从DBNull强制转换为其他类型? [英] Object cannot be cast from DBNull to other types?

查看:301
本文介绍了无法将对象从DBNull强制转换为其他类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他在我的项目中使用了动态功能,并且遇到了我上面提到的错误.

 如果(((偏移量+ BUFFER_LENGTH)>  = System.Convert. ToInt32(LengthOutParam.Value))// 此行出现错误

                   SizeParam.Value = System.Convert.ToInt32(LengthOutParam.Value)-偏移量;
               其他 SizeParam.Value = BUFFER_LENGTH; 


谁能建议我.如何解决此错误. 谢谢

这样的错误是很容易理解的.不过,如果需要,可以使用简单的 如果(LengthOutParam.Value!= DBNull.Value) { 如果((偏移量+ BUFFER_LENGTH)> = System.Convert.ToInt32(LengthOutParam.Value)) SizeParam.Value = System.Convert.ToInt32(LengthOutParam.Value)-偏移量; 其他 SizeParam.Value = BUFFER_LENGTH; }


您正在将DBnull类型转换为整数. 只需检查Value是否不是DBNull ,如果不是,则转换为整数.

示例-

if((Offset + BUFFER_LENGTH) >= (LengthOutParam.Value == DbNull)?0:System.Convert.ToInt32(LengthOutParam.Value))


我假设LengthOutParam是数据库的输出参数.

之所以出现此错误,是因为在您的sp中没有为此参数分配值.

并且默认值为DBNull.


hi am using dynamic in my project and am getting error which i mentioned above.

if((Offset + BUFFER_LENGTH) >= System.Convert.ToInt32(LengthOutParam.Value))//this line am getting error

                   SizeParam.Value = System.Convert.ToInt32(LengthOutParam.Value) - Offset;
               else SizeParam.Value = BUFFER_LENGTH;


can any one suggest me.how to solve this error.??
thanks

Error as such is quite self explanatory. Still, if needed, a simple Google search[^] would have suggested you to check for the DBNULL validation before using it.

Try:

if (LengthOutParam.Value != DBNull.Value)
{
   if((Offset + BUFFER_LENGTH) >= System.Convert.ToInt32(LengthOutParam.Value))
             SizeParam.Value = System.Convert.ToInt32(LengthOutParam.Value) - Offset;
   else SizeParam.Value = BUFFER_LENGTH;
}


You are converting a DBnull type to an interger .
Simply check if Value is not DBNull and if not, convert to integer.

Example -

if((Offset + BUFFER_LENGTH) >= (LengthOutParam.Value == DbNull)?0:System.Convert.ToInt32(LengthOutParam.Value))


I assume that LengthOutParam is output parameter from database.

You are getting this error because in your sp this parameter hasn''t been assigned a value.

And the default value is DBNull.


这篇关于无法将对象从DBNull强制转换为其他类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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