问题描述
usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;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;publicpartialclasslessons:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="selectunamefromuserswhereuid='"+Session["uid"].ToString()+"'";SqlDataReaderdr=cmd.ExecuteReader();if(dr.Read()){Session["uid"].ToString();Session["uname"]=dr[0].ToString();}conn.Close();if(IsPostBack){conn.Open();SqlCommandcmd1=newSqlCommand();cmd1.Connection=conn;cmd1.CommandText="selectaddress,teacherfromcoursewherename='"+DropDownList2.SelectedItem.Value+"'";SqlDataReaderdr1=cmd1.ExecuteReader();if(dr1.Read()){Label2.Text=dr1[0].ToString();Label1.Text=dr1[1].ToString();}conn.Close();}}protectedvoidButton1_Click(objectsender,EventArgse){//数据库的连接SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();//存储过程实例化SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="insertintoscore(cname,lesson,address,tname,sid,sname)values('"+DropDownList1.SelectedItem.Value+"','"+DropDownList2.SelectedItem.Value+"','"+Label2.Text+"','"+Label1.Text+"','"+Session["uid"].ToString()+"','"+Session["uname"].ToString()+"')";inti=cmd.ExecuteNonQuery();if(i==0){Response.Write("<scriptlanguage='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");}else{Session["teacher"]=Label1.Text.ToString();Session["address"]=Label2.Text.ToString();Response.Write("<scriptlanguage='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");}conn.Close();}}其中红色字体部分提示为:未将对象引用设置到对象的实例。
解决方案
解决方案二:
cmd.CommandText="selectunamefrom[users]where[uid]='"+Session["uid"].ToString()+"'";
解决方案三:
if(dr.Read()){Session["uid"].ToString();//什么意思Session["uname"]=dr[0].ToString();}conn.Close();if(IsPostBack)//是不是少个!我彻底服了
解决方案四:
还有你Page_Load里面用了SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";Button1_Click也用你为什么不写个数据库连接类或者方法web.Config文件里。你这样代码阅读性太差了
解决方案五:
这些写在类中#region//SQL连接数据库publicSqlConnectionGetCon(){stringconnstr="DataSource=.;DataBase=graph;UID=sa;PWD=sa";SqlConnectionmycon=newSqlConnection(connstr);returnmycon;}#endregion#region//SQL返回数据表publicDataTableGetTb(stringsqlstr){DataTablemytb=newDataTable();SqlConnectionmycon=this.GetCon();mycon.Open();SqlDataAdaptermyadpt=newSqlDataAdapter(sqlstr,mycon);myadpt.Fill(mytb);myadpt.Dispose();mycon.Close();mycon.Dispose();returnmytb;}#endregion#region//SQL语句执行publicvoidgetcmd(stringsqlstr){SqlConnectionsqlcon=this.GetCon();sqlcon.Open();SqlCommandmycmd=newSqlCommand(sqlstr,sqlcon);mycmd.ExecuteNonQuery();mycmd.Dispose();sqlcon.Close();sqlcon.Dispose();}#endregion页面中调用
解决方案六:
感谢各位对我的帮助,可是改了还是不行呢?我是编程的菜菜鸟,各位高手能否加我QQ帮忙一下:758422083非常感谢!!!
解决方案七:
usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;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;publicpartialclasslessons:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="selectunamefrom[users]where[uid]='"+Session["uid"].ToString()+"'";SqlDataReaderdr=cmd.ExecuteReader();if(dr.Read()){Session["uname"]=dr[0].ToString();}conn.Close();if(!IsPostBack){conn.Open();SqlCommandcmd1=newSqlCommand();cmd1.Connection=conn;cmd1.CommandText="selectaddress,teacherfromcoursewherename='"+DropDownList2.SelectedItem.Value+"'";SqlDataReaderdr1=cmd1.ExecuteReader();if(dr1.Read()){Label2.Text=dr1[0].ToString();Label1.Text=dr1[1].ToString();}conn.Close();}}protectedvoidButton1_Click(objectsender,EventArgse){//数据库的连接SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();//存储过程实例化SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="insertintoscore(cname,lesson,address,tname,sid,sname)values('"+DropDownList1.SelectedItem.Value+"','"+DropDownList2.SelectedItem.Value+"','"+Label2.Text+"','"+Label1.Text+"','"+Session["uid"].ToString()+"','"+Session["uname"].ToString()+"')";inti=cmd.ExecuteNonQuery();if(i==0){Response.Write("<scriptlanguage='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");}else{Session["teacher"]=Label1.Text.ToString();Session["address"]=Label2.Text.ToString();Response.Write("<scriptlanguage='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");}conn.Close();}protectedvoidDropDownList2_SelectedIndexChanged(objectsender,EventArgse){}}浏览器端提示的错误是这样的:“/学生信息管理系统”应用程序中的服务器错误。--------------------------------------------------------------------------------未将对象引用设置到对象的实例。说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.NullReferenceException:未将对象引用设置到对象的实例。源错误:行20:SqlCommandcmd=newSqlCommand();行21:cmd.Connection=conn;行22:cmd.CommandText="selectunamefrom[users]where[uid]='"+Session["uid"].ToString()+"'";行23:SqlDataReaderdr=cmd.ExecuteReader();行24:if(dr.Read())源文件:e:毕业设计相关下载的资料ASP.net和SQL2005设计的学生信息管理系统学生信息管理系统lessons.aspx.cs行:22堆栈跟踪:[NullReferenceException:未将对象引用设置到对象的实例。]lessons.Page_Load(Objectsender,EventArgse)ine:毕业设计相关下载的资料ASP.net和SQL2005设计的学生信息管理系统学生信息管理系统lessons.aspx.cs:22System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtrfp,Objecto,Objectt,EventArgse)+31System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Objectsender,EventArgse)+68System.Web.UI.Control.OnLoad(EventArgse)+88System.Web.UI.Control.LoadRecursive()+74System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+3037--------------------------------------------------------------------------------版本信息:Microsoft.NETFramework版本:2.0.50727.42;ASP.NET版本:2.0.50727.42
解决方案八:
引用6楼zzz2800的回复:
usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSyst……
你现在sql里面执行一下看成功不
解决方案九:
引用7楼chinajiyong的回复:
引用6楼zzz2800的回复:usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;usingSystem.Collections;usingSystem.Web;usingSystem.Web.Security;usingSyst……
你先把下面的注释了。在调试看什么错误if(!IsPostBack){conn.Open();SqlCommandcmd1=newSqlCommand();cmd1.Connection=conn;cmd1.CommandText="selectaddress,teacherfromcoursewherename='"+DropDownList2.SelectedItem.Value+"'";SqlDataReaderdr1=cmd1.ExecuteReader();if(dr1.Read()){Label2.Text=dr1[0].ToString();Label1.Text=dr1[1].ToString();}conn.Close();}
解决方案十:
usingSystem;usingSystem.Data;usingSystem.Data.SqlClient;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;publicpartialclasslessons:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="selectunamefromuserswhereuid='"+Session["uid"].ToString()+"'";SqlDataReaderdr=cmd.ExecuteReader();if(dr.Read()){Session["uname"]=dr[0].ToString();}conn.Close();/*if(!IsPostBack){conn.Open();SqlCommandcmd1=newSqlCommand();cmd1.Connection=conn;cmd1.CommandText="selectaddress,teacherfromcoursewherename='"+DropDownList2.SelectedItem.Value+"'";SqlDataReaderdr1=cmd1.ExecuteReader();if(dr1.Read()){Label2.Text=dr1[0].ToString();Label1.Text=dr1[1].ToString();}conn.Close();}*/}protectedvoidButton1_Click(objectsender,EventArgse){//数据库的连接SqlConnectionconn=newSqlConnection();conn.ConnectionString="DataSource=.\SQLEXPRESS;InitialCatalog=usersinfo;IntegratedSecurity=SSPI";conn.Open();//存储过程实例化SqlCommandcmd=newSqlCommand();cmd.Connection=conn;cmd.CommandText="insertintoscore(cname,lesson,address,tname,sid,sname)values('"+DropDownList1.SelectedItem.Value+"','"+DropDownList2.SelectedItem.Value+"','"+Label2.Text+"','"+Label1.Text+"','"+Session["uid"].ToString()+"','"+Session["uname"].ToString()+"')";inti=cmd.ExecuteNonQuery();if(i==0){Response.Write("<scriptlanguage='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");}else{Session["teacher"]=Label1.Text.ToString();Session["address"]=Label2.Text.ToString();Response.Write("<scriptlanguage='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");}conn.Close();}protectedvoidDropDownList2_SelectedIndexChanged(objectsender,EventArgse){}}注释了还是这样还是红色部分的代码提示有错