首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

为什么总出现"未将对象引用设置到对象的实例"该怎么处理

2012-01-05 
为什么总出现未将对象引用设置到对象的实例我在一个类中写了如下方法:publicSqlDataReaderGetUserID(str

为什么总出现"未将对象引用设置到对象的实例"
我在一个类中写了如下方法:

public   SqlDataReader   GetUserID(string   UserID)
{
SqlConnection   myConnection   =   new   SqlConnection(MyConn.strSqlConn);
SqlCommand   myCommand   =   new   SqlCommand( "GetUserID ",   myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myCommand.Parameters.Add(new   SqlParameter( "@userid ",SqlDbType.VarChar));
myCommand.Parameters[ "@userid "].Value=UserID;
try
{
myConnection.Open();
  SqlDataReader   dr   =   myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return   dr;
}
catch
{
return   null;
}
}
然后我在另一个WEB面中调用此方法时:出现:未将对象引用设置到对象的实例

行   495:public   SqlDataReader   GetUserNameByUserID(string   UserID)
行   496:{
行   497:SqlConnection   myConnection   =   new   SqlConnection(MyConn.strSqlConn);   //提示这行出错

谁能告诉我是为什么?应该怎么写


[解决办法]
代码帖出来看,还有如果你的连接字符串是写在web.config中的,要注意字符串的中空格,刚才偶也是出这个错,就是因为多了一个空格!

热点排行