C#未将对象引用设置到对象的实例是什么意思?

这段修改用户密码的代码:
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["DBConnectionString"]);
SqlCommand selectcmd = new SqlCommand("select * from Users where userId='" + Session["userId"].ToString() + "' and userPwd='" + txtOldPwd.Text.Trim() + "'", conn);//这句出现“未将对象引用设置到对象的实例”错误
try
{
conn.Open();
SqlDataReader sdr = selectcmd.ExecuteReader();
if (sdr.Read())
{
sdr.Close();
SqlCommand updatecmd = new SqlCommand("update Users set userPwd='" + txtNewPwd.Text.Trim() + "' where userId='" + Session["userId"].ToString() + "'", conn);
int i = updatecmd.ExecuteNonQuery();
if (i > 0)
{
Response.Write("<script language=javascript>alert('密码修改成功!')</script>");
}
else
{
Response.Write("<script language=javascript>alert('密码修改失败!')</script>");
}
}
else
{
Response.Write("<script language=javascript>alert('旧密码错误!')</script>");
}
}
catch (System.Exception ee)
{
Response.Write("<script language=javascript>alert('" + ee.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
我在其他的页面也是这么用的,可是为什么到这个页面就不行了呢?望高手指教!


Session["userId"].ToString()
这句造成的
如果Session["userId"]为null的话就有这错误
你可以这样
string UserId="";
if(Session["userId"]!=null)UserId=Session["userId"].ToString();
另外 建议不要使用try,这是个不好的习惯
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-07-24
+ txtOldPwd.Text.Trim() +

你要 + 什么? userPwd= 'txtOldPwd.Text.Trim()' 就可以了啊。
第2个回答  2009-07-24
顶楼上的.你加载页面的时候.Session一般是空的.没有数据.这样的话就会造成空引用异常.
相似回答