问题描述
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.SqlClient;publicpartialclassModPwd:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidbtn_mod_Click(objectsender,EventArgse){if(CheckPwd()&&Page.IsValid){ModPassword();Response.Redirect("SuccessEdit.aspx");}}publicboolCheckPwd(){stringpassword=tbx_orignpwd1.Text;if(password==""){lbl_msg.Visible=true;lbl_msg.Text="没有输入原始密码";returnfalse;}else{//读取数据库连接字符串stringsettings=Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);//创建数据库连接SqlConnectionmyconn=newSqlConnection(settings);//打开数据库连接myconn.Open();SqlCommandmycmd=newSqlCommand("UserPwd",myconn);//设定命令类型为存储过程mycmd.CommandType=CommandType.StoredProcedure;//参数SqlParameterparaUserid=newSqlParameter("@UserID",SqlDbType.Int);paraUserid.Value=Convert.ToInt32(Session["UserID"].ToString());mycmd.Parameters.Add(paraUserid);SqlParameterparaPassword=newSqlParameter("@Password",SqlDbType.NVarChar,12);paraPassword.Value=tbx_orignpwd1.Text.Trim();mycmd.Parameters.Add(paraPassword);SqlDataReaderdr=mycmd.ExecuteReader();if(dr.Read()){myconn.Close();returntrue;}else{lbl_msg.Visible=true;lbl_msg.Text="原始密码不正确!";returnfalse;}}}publicvoidModPassword(){//读取数据库连接字符串stringsettings=Convert.ToString(ConfigurationManager.ConnectionStrings["SqlServices"]);//创建数据库连接SqlConnectionmyconn=newSqlConnection(settings);//打开数据库连接myconn.Open();SqlCommandmycmd=newSqlCommand("ModPwd",myconn);//设定命令类型为存储过程mycmd.CommandType=CommandType.StoredProcedure;//参数SqlParameterparaUserid=newSqlParameter("@UserID",SqlDbType.Int);paraUserid.Value=Convert.ToInt32(Session["UserID"].ToString());mycmd.Parameters.Add(paraUserid);SqlParameterparaPassword=newSqlParameter("@Password",SqlDbType.NVarChar,12);paraPassword.Value=tbx_newpwd1.Text.Trim();mycmd.Parameters.Add(paraPassword);mycmd.ExecuteNonQuery();myconn.Close();}protectedvoidbtn_back_Click(objectsender,EventArgse){Response.Redirect("Default.aspx");}}一直出现:未将对象引用设置到对象的实例。说明:执行当前Web请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例。源错误:行50://参数行51:SqlParameterparaUserid=newSqlParameter("@UserID",SqlDbType.Int);行52:paraUserid.Value=Convert.ToInt32(Session["UserID"].ToString());行53:mycmd.Parameters.Add(paraUserid);行54:不知道哪里出问题了
解决方案
解决方案二:
paraUserid.Value=Convert.ToInt32(Session["UserID"].ToString());把.ToString()去掉。
解决方案三:
Session["UserID"]是否得到值?
解决方案四:
Session["UserID"]为空吧?最好先判断一下是否有设置if(Session["UserID"]==null){return;}
解决方案五:
Session["UserID"]为空,你最好先判断哈,也可能是session丢失了。。。
解决方案六:
是的,请判断Session["UserID"]是否为空if(Session["UserID"]==null){return;}