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();
    }}
其中红色字体部分提示为:未将对象引用设置到对象的实例。

解决方案 »

  1.   

    cmd.CommandText = "select uname from [users] where [uid] = '" + Session["uid"].ToString() + "'";
      

  2.   

     if (dr.Read())
      {
      Session["uid"].ToString();//什么意思
      Session["uname"] = dr[0].ToString();
      }
      conn.Close();
      if(IsPostBack)//是不是少个!
    我彻底服了
      

  3.   

    还有你Page_Load里面用了
      SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=usersinfo;Integrated Security=SSPI";
    Button1_Click也用
    你为什么不写个数据库连接类
    或者方法web.Config文件里。你这样代码阅读性太差了
      

  4.   

    这些写在类中
     #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页面中调用
      

  5.   

    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["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 
      

  6.   

    你现在sql里面执行一下看成功不
      

  7.   

    你先把下面的注释了。在调试看什么错误
     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();
      }
      

  8.   

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