Object reference not set to an instance of an object
****源代碼
int id=0;
SqlCommand objcmd = new SqlCommand("spcheckmprno", objcon);
objcmd.CommandType = CommandType.StoredProcedure;
SqlParameter objparam=new SqlParameter("@mprno",SqlDbType.Char);
objparam.Value = mprno;
objcmd.Parameters.Add(objparam);
try
{
objcon.Open();
id = Convert.ToInt16(objcmd.ExecuteScalar().ToString());
objcon.Close();
}
catch (Exception e)
{
throw e;
}
***出錯:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 91: catch (Exception e)
Line 92: {
Line 93: throw e;
Line 94: }
Line 95: if (id == 0)
***刪除這行後就可以
//id = Convert.ToInt16(objcmd.ExecuteScalar().ToString());
可能是返回的值為NULL就出錯
有什麼辦法解決呀
问题点数:20、回复次数:4Top
1 楼zsb78(killer)回复于 2005-02-25 16:21:47 得分 0
有值就不會出錯
如何處理空值呀Top
2 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:40:31 得分 20
string str = Convert.ToString(objcmd.ExecuteScalar());
if (str != "")//这样没有值或者值为空的两种情况都考虑了
id = Convert.ToInt16(str);Top
3 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:41:49 得分 0
Convert.ToString方法如果传入参数为Null,是不会出错的,返回空字符串。Top
4 楼xixigongzhu(夕夕公主(※快乐在于自己※))回复于 2005-02-25 16:42:49 得分 0
但Convert.ToInt16方法的参数如果为Null或者空字符串是会出错的。Top
相关问题
- Object reference not set to an instance of an object.
- Object reference not set to an instance of an object.
- error Message:Object reference not set to an instance of an object.
- "Object reference not set to an instance of an object":奇怪的错误
- 高分求:Object reference not set to an instance of an object
- object reference not set to an instance of an object是什麽錯誤?
- Object reference not set to an instance of an object.一般是什么原因造成?
- 我安装OSmartDeviceFramework14的时候他说bject reference not set to an instance of an object
- Page_Load中出现的问题,出现这个错误:Object reference not set to an instance of an object
- 请教 :修改datagridview的数据 更新到数据库 代码如下 (object reference not set an instance of an object? )




