我是在是找不到这段代码错在何处了

来源:百度知道 编辑:UC知道 时间:2024/07/06 12:59:11
他总是提示未将对象引用设置到对象的实例。

if (!IsPostBack)
{
string id = Request["id"].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
con.Open();
string selectstring = "select * from [user] where id='" + id + "'";
SqlCommand cmd1 = new SqlCommand(selectstring,con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
this.lboperator.Text = dr["operator"].ToString();
}
con.Close();
}

有可能是这句错了
string id = Request["id"].ToString();

这样, 我用的是.Net 2005

string id = "0";
if (!string.IsNullOrEmpty(Request["id"]))
{
string id = Request["id"].ToString();
}

如果是2003就这样
string id = "0";
if (Request["id"] != null)
{
string id = Request["id"].ToString();
}

没引用到实例应该会给出具体出错的位置吧?

可能出现的三个问题:
1、ConfigurationManager.ConnectionStrings["conn"].ConnectionString。确定你的Web.config文件中有connectionStrings节,并且名称为conn。注意,不是写在APP里面的。是单独的connectionStrings节。

2.确定你的数据表里面有operator字段。

3.你没判断QueryString的id是否存在……

Request["id"] 是null
不可能ToString()

1,Request["id"]可能为null
2,连接字符串读取错误!
3,sql语句错误!

string selectstring = "select * from [user] where id='" + id + "'";
你数据库里的ID是字符串型嘛