日期:2014-05-18  浏览次数:20384 次

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
string   id=Request.QueryString[ "id "].Trim();

        string   Sqlstr   =   "select   *   from   adminas   where   ID= ' "+id+ " ' ";
我在连接里面传了一id值如果为空的时间就出现这样的错误
异常详细信息:   System.NullReferenceException:   未将对象引用设置到对象的实例

有什么办法解决
   


------解决方案--------------------
为空肯定会出现那样的问题
可以判断有没有传值

------解决方案--------------------
1.
if (Request.QueryString.Count > 0)
{
string Sqlstr = "select * from adminas where ID= ' "+id+ " ' "; //处理语句
}
else
{
//处理语句 这里我是直接把ID写死
}


2.
if(id=null)
{
id=1
}
else
{
string Sqlstr = "select * from adminas where ID= ' "+id+ " ' "; //处理语句
}
------解决方案--------------------
2.
if(id==null) //这里少写一个等号……
{
id=1
}
else
{
string Sqlstr = "select * from adminas where ID= ' "+id+ " ' "; //处理语句
}
------解决方案--------------------
if(!object.Equals(Request.QueryString[ "id "],null) && Request.QueryString[ "id "].ToString()!=string.Empty){
......
}
------解决方案--------------------
1.
if (Request.QueryString.Count > 0)
{
string Sqlstr = "select * from adminas where ID= ' "+id+ " ' "; //处理语句
}
else
{
//处理语句 这里我是直接把ID写死
}

这个可以不?
------解决方案--------------------
楼主id为空是id= ' '还是id=null?
如果是null 则不应该写成
select * from adminas where ID=null
而应该为
select * from adminas where ID is null
另可以将定义一个@id变量,然后付值(与程序中相同的数值)然后再sql中直接执行语句
select * from adminas where ID=@id
看看有什么结果.
------解决方案--------------------
应该这样判断
int id=(Request.QueryString[ "id "]==null)?1:Convert.ToInt32(Request.QueryString[ "id "]);
------解决方案--------------------
楼上的老兄顶的好。言简意赅。值得我辈学习。