单击选课下拉按钮时出现错误

问题描述

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){}}注释了还是这样还是红色部分的代码提示有错

时间: 2024-10-21 02:58:20

单击选课下拉按钮时出现错误的相关文章

初始化客户代号下拉框时

问题描述 packageinternal.cus;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;importjavax.swing.JButton;importjavax.swing.JComboBox;importjavax.swing.JFrame;impor

vs2005 release/debug下拉按钮解决方案配置按钮设置

自从重新安装了VS2005后,发现原来常用的release/debug下拉按钮不见了,急啊,不方便啊,如下图: 自己找了半天也没找出来,后来问了下同事,发现这个菜单按钮名字叫:解决方案配置按钮. 有了这名字之后,一搜寻,立即就有解决方案了: 1.选择"工具". 2.选择"自定义". 3.选择"命令"选项卡. 4.从左边类别里面找到"生成". 5.选中"生成"后,在右面选择"解决方案配置"

控件随窗体等比例缩放,怎么改变combo控件下拉按钮的大小?

问题描述 控件随窗体等比例缩放,怎么改变combo控件下拉按钮的大小? 控件随窗体等比例缩放,怎么改变combo控件下拉按钮的大小?为什么它的大小不会自动缩放?

listview刷新-ListView下拉刷新时偶尔动画会卡住

问题描述 ListView下拉刷新时偶尔动画会卡住 listview的下拉刷新相信大家都做过,我这次用的是谷歌官方的swiperefresh,在下拉刷新的时候遇到一个问题,就是偶尔刷新动画会无限重复,不会自己消失,手动也停不下来....如图,这个动画会一直重复,以前用自定义listview时也遇到过这个问题,不知道该如何解决,求大神指点迷津! 解决方案 public void onRefresh() { new Handler().postDelayed(new Runnable() { @Ov

android-listView下拉刷新时,为什么同时触发点击事件,如何让下拉时不触发点击事件

问题描述 listView下拉刷新时,为什么同时触发点击事件,如何让下拉时不触发点击事件 /** * Touch事件 */ @Override public boolean onTouchEvent(MotionEvent ev) { Log.e("666","555"); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: //按下时Y坐标 // 正在刷新不能移动 if (currentState ==

安卓应用开发-安卓点击下拉按钮弹出编辑界面布局,这种布局叫什么名字

问题描述 安卓点击下拉按钮弹出编辑界面布局,这种布局叫什么名字 不是spinner那种.开发-安卓点击下拉按钮弹出编辑界面布局,这种布局叫什么名字-点击按钮弹出下拉菜单"> 解决方案 http://www.javaapk.com/source/6170.html

我的echarts地图主题不能修改是怎么回事啊?点击下拉框时地图没效果,也不刷新??????

问题描述 我的echarts地图主题不能修改是怎么回事啊?点击下拉框时地图没效果,也不刷新?????? 解决方案 <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</title> <script> var myChart; function changeTheme(self){ //var te = require('echarts/theme

supertabcontrol 右侧下拉按钮如何隐藏

问题描述 dotnetbar里面有个supertabcontrol非常好用,但是控件右侧下拉按钮如果想隐藏它应该做?请各位大虾给点建议如下图所示 解决方案 解决方案二:无意间找到了和大家说一下是MenuBox下面的子菜单AutoHide设为True就可以了.Mark一下

下拉刷新空白-listview下拉刷新时空白一片 切换一下再返回才能见效 怎么搞

问题描述 listview下拉刷新时空白一片 切换一下再返回才能见效 怎么搞 一定要用子线程去刷新吗?我是用pulltorefreshlistview的listener实现的两个上拉和下拉的方法进行刷新操作的 解决方案 pulltorefreshlistview不是有AsyncTask吗? 再对照一下例子http://www.cnblogs.com/jshen/p/4097445.html 解决方案二: 不是子线程刷新,是把你的数据请求放到线程里面,接收到数据后,交给前端去填充界面,你下拉时,触