using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class lessons : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select uname from users where uid = '" + Session["uid"].ToString() + "'"; SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["uid"].ToString();
Session["uname"] = dr[0].ToString();
}
conn.Close();
if(IsPostBack)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "select address,teacher from course where name = '" + DropDownList2.SelectedItem.Value + "'";
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
Label2.Text = dr1[0].ToString();
Label1.Text = dr1[1].ToString();
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
//存储过程实例化
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into score(cname,lesson,address,tname,sid,sname) values('" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + Label2.Text + "','" + Label1.Text + "','" + Session["uid"].ToString() + "','" + Session["uname"].ToString() + "')";
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
Response.Write("<script language='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");
}
else
{
Session["teacher"] = Label1.Text.ToString();
Session["address"] = Label2.Text.ToString();
Response.Write("<script language='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");
}
conn.Close();
}}
其中红色字体部分提示为:未将对象引用设置到对象的实例。
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class lessons : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select uname from users where uid = '" + Session["uid"].ToString() + "'"; SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["uid"].ToString();
Session["uname"] = dr[0].ToString();
}
conn.Close();
if(IsPostBack)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "select address,teacher from course where name = '" + DropDownList2.SelectedItem.Value + "'";
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
Label2.Text = dr1[0].ToString();
Label1.Text = dr1[1].ToString();
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
//存储过程实例化
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into score(cname,lesson,address,tname,sid,sname) values('" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + Label2.Text + "','" + Label1.Text + "','" + Session["uid"].ToString() + "','" + Session["uname"].ToString() + "')";
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
Response.Write("<script language='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");
}
else
{
Session["teacher"] = Label1.Text.ToString();
Session["address"] = Label2.Text.ToString();
Response.Write("<script language='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");
}
conn.Close();
}}
其中红色字体部分提示为:未将对象引用设置到对象的实例。
解决方案 »
- 请求的操作无法在使用用户映射区域打开的文件上执行。
- 如何实现支持多种格式的流媒体播放器?基于asp.net的
- vs2008的TreeView中,如何在选中的节点前面(或者后面)增加一个节点,或者一个子节点?
- Literal控件如何设置大小啊?
- JS调用图片问题?
- 问一个关于asp.net控件布局的问题
- 很菜的问题,求助!
- 发现ASP.NET里表单验证的时候有矛盾!!大家进来瞧瞧.
- 我在运行时,页面提示错误,可是它很快就跳到百度里去了,我都没时间看什么错误,怎么回事啊?怎么解决?
- 防止驻入式攻击的一个疑问(谢谢帮顶)
- form 的 runat=server问题
- asp.net 2.0中如何实现上传文件与下载文件求具体的代码
{
Session["uid"].ToString();//什么意思
Session["uname"] = dr[0].ToString();
}
conn.Close();
if(IsPostBack)//是不是少个!
我彻底服了
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
Button1_Click也用
你为什么不写个数据库连接类
或者方法web.Config文件里。你这样代码阅读性太差了
#region //SQL连接数据库
public SqlConnection GetCon()
{
string connstr = "Data Source=.;DataBase=graph;UID=sa;PWD=sa";
SqlConnection mycon = new SqlConnection(connstr);
return mycon;
}
#endregion #region //SQL返回数据表
public DataTable GetTb(string sqlstr)
{
DataTable mytb = new DataTable();
SqlConnection mycon = this.GetCon();
mycon.Open();
SqlDataAdapter myadpt = new SqlDataAdapter(sqlstr, mycon);
myadpt.Fill(mytb);
myadpt.Dispose();
mycon.Close();
mycon.Dispose();
return mytb;
}
#endregion #region //SQL语句执行
public void getcmd(string sqlstr)
{
SqlConnection sqlcon = this.GetCon();
sqlcon.Open();
SqlCommand mycmd = new SqlCommand(sqlstr, sqlcon);
mycmd.ExecuteNonQuery();
mycmd.Dispose();
sqlcon.Close();
sqlcon.Dispose();
}
#endregion页面中调用
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class lessons : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select uname from [users] where [uid] = '" + Session["uid"].ToString() + "'";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{ Session["uname"] = dr[0].ToString();
}
conn.Close();
if(!IsPostBack)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "select address,teacher from course where name = '" + DropDownList2.SelectedItem.Value + "'";
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
Label2.Text = dr1[0].ToString();
Label1.Text = dr1[1].ToString();
}
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
//存储过程实例化
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into score(cname,lesson,address,tname,sid,sname) values('" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + Label2.Text + "','" + Label1.Text + "','" + Session["uid"].ToString() + "','" + Session["uname"].ToString() + "')";
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
Response.Write("<script language='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");
}
else
{
Session["teacher"] = Label1.Text.ToString();
Session["address"] = Label2.Text.ToString();
Response.Write("<script language='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");
}
conn.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{ }
}浏览器端提示的错误是这样的:
“/学生信息管理系统”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 20: SqlCommand cmd = new SqlCommand();
行 21: cmd.Connection = conn;
行 22: cmd.CommandText = "select uname from [users] where [uid] = '" + Session["uid"].ToString() + "'";
行 23: SqlDataReader dr = cmd.ExecuteReader();
行 24: if (dr.Read())
源文件: e:\毕业设计相关\下载的资料\ASP.net和SQL 2005设计的学生信息管理系统\学生信息管理系统\lessons.aspx.cs 行: 22 堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
lessons.Page_Load(Object sender, EventArgs e) in e:\毕业设计相关\下载的资料\ASP.net和SQL 2005设计的学生信息管理系统\学生信息管理系统\lessons.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +31
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +68
System.Web.UI.Control.OnLoad(EventArgs e) +88
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3037
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42
if(!IsPostBack)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "select address,teacher from course where name = '" + DropDownList2.SelectedItem.Value + "'";
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
Label2.Text = dr1[0].ToString();
Label1.Text = dr1[1].ToString();
}
conn.Close();
}
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class lessons : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select uname from users where uid = '" + Session["uid"].ToString() + "'"; SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["uname"] = dr[0].ToString();
}
conn.Close();
/*if(!IsPostBack)
{
conn.Open();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = conn;
cmd1.CommandText = "select address,teacher from course where name = '" + DropDownList2.SelectedItem.Value + "'";
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.Read())
{
Label2.Text = dr1[0].ToString();
Label1.Text = dr1[1].ToString();
}
conn.Close();
}*/
}
protected void Button1_Click(object sender, EventArgs e)
{
//数据库的连接
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
conn.Open();
//存储过程实例化
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "insert into score(cname,lesson,address,tname,sid,sname) values('" + DropDownList1.SelectedItem.Value + "','" + DropDownList2.SelectedItem.Value + "','" + Label2.Text + "','" + Label1.Text + "','" + Session["uid"].ToString() + "','" + Session["uname"].ToString() + "')";
int i = cmd.ExecuteNonQuery();
if (i == 0)
{
Response.Write("<script language='javascript'>alert('选课失败!');window.location.href='lessons.aspx';</script>");
}
else
{
Session["teacher"] = Label1.Text.ToString();
Session["address"] = Label2.Text.ToString();
Response.Write("<script language='javascript'>alert('选课成功!');window.location.href='lessons.aspx';</script>");
}
conn.Close();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{ }
}注释了还是这样还是红色部分的代码提示有错