当前位置:编程学习 > C#/ASP.NET >>

关于未将对象引用设置到对象的实例。求解!

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)
    {

    }
}

这是一个有两个dropdownlist下拉按钮的控件的选课的页面,每次点击第二个下拉按钮的时候就报错:未将对象引用设置到对象的实例。后来我改了连接数据源,运行的时候连网页都打不开了,直接显示报错的页面了。。各位老鸟,请问这个怎么解决呢???求解... --------------------编程问答-------------------- tostring出错
 如果 你得到的内容是null 那么方法就会出错

建议 检查是否正确
其次 用强转 convert.tostring --------------------编程问答-------------------- Session["uid"]是在页面加载前就赋值了么?

如果没有赋值,就会值为null,报的就是未将对象引用设置到对象的实例,反正见着这个错,你就想想哪里的值为null了 --------------------编程问答--------------------
引用 2 楼 jjkk168 的回复:
Session["uid"]是在页面加载前就赋值了么?

如果没有赋值,就会值为null,报的就是未将对象引用设置到对象的实例,反正见着这个错,你就想想哪里的值为null了

有没有可能你的session丢了。你自己调试看看 --------------------编程问答-------------------- Session["uid"]是不是空的? --------------------编程问答-------------------- Session["uid"]是空的 --------------------编程问答-------------------- 检查一下 Session["uid"].ToString()  这个的赋值先后时间 确认下是否在调用的时候一定有值、。 --------------------编程问答-------------------- 断点,看看Session["uid"]的值 --------------------编程问答-------------------- 这句就有两个对象。cmd你上面刚实例化
只可能是Session["uid"]是空的 --------------------编程问答-------------------- 加一个判断
if(Session["uid"]!=null) --------------------编程问答-------------------- Session["uid"] --------------------编程问答-------------------- 嗯~嗯
Session["uid"] --------------------编程问答-------------------- DropDownList2.SelectedItem.Value  是否为空  --------------------编程问答-------------------- 出现这个错的一般都某个数据为空,我一般的解决方法是设置断点调试,不要着急如果查不出多调试几次。 --------------------编程问答-------------------- 就是没取到值的问题,断点调试
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,