private void ReturnUserData(int iRet)
        {
            string strSQL = "";
            LinkDataBase link = new LinkDataBase();
            DataSet ds = new DataSet();
            DataTable dt;
            if (iRet != 0)
            {
                strSQL = "select * from SysUser where SysUserID = '" + iRet + "'";
            }
            ds = link.SelectDB(strSQL);
            if (ds.Tables[0].Rows.Count > 0)
            {
                dt = ds.Tables[0];
                this.textBox1.Text = dt.Rows[0]["SysUserCode"].ToString();
                this.textBox2.Text = dt.Rows[0]["SysUserName"].ToString();
                this.textBox3.Text = dt.Rows[0]["Psw"].ToString();
                this.textBox4.Text = dt.Rows[0]["Email"].ToString();
                this.textBox5.Text = dt.Rows[0]["Tel"].ToString();
                this.textBox6.Text = dt.Rows[0]["Address"].ToString();
                this.comboBox1.Text = dt.Rows[0]["DeptID"].ToString();
                this.comboBox2.Text = dt.Rows[0]["Sex"].ToString();
            }
        }为什么老是在运行到this.textBox1.Text = dt.Rows[0]["SysUserCode"].ToString();就出现“未将对象引用设置到对象的实例”啊,请帮帮,急呀!

解决方案 »

  1.   

    根据代码和错误提示唯一可能性
    不存在SysUserCode列??
      

  2.   

    SysUserCode写错了,肯定..仔细查一查.
      

  3.   

    SysUserCode没写错,所有字段没错,我现在就是this.textBox1.Text="3";这样付值也出现相同的错误,请高手指点。急呀!非常急
      

  4.   

    编译了,我公司现在所有人都找不出原因,程序编译能通过。现在就是不知道文本框是怎么回事,为什么不能对文本框付值。
    如果是这样 string s = dt.Rows[0]["SysUserCode"].ToString();就不会有错,请高手指点。非常感谢。
      

  5.   

    SysUserCode没写错,所有字段没错,我现在就是this.textBox1.Text="3";这样付值也出现相同的错误,请高手指点。急呀!非常急
    =====
    调试
    看看this.textBox1是否为空
    Respone.Write( this.Text==null )
      

  6.   

    没错,是为空的,所有文本框都为空!我这是C#写的,是C/S结构的程序,请指点。真的非常感谢!!!
      

  7.   

    hdt(倦怠) 你肯定是个高手,请帮帮忙!!!先谢拉
      

  8.   

    没错,是为空的,所有文本框都为空!我这是C#写的,是C/S结构的程序
    ======================
    是不是你手动修改过vs.net 自动生成的代码??
      

  9.   

    我的做做法是,在ListView 上显示用户信息,当要修改某一个用户,双击,进入要修改的窗体,然后传一个ID号 赋值给iRet。然后传到这个窗体,进行数据库查询。把这个用户信息查出来,显示在文本框和下拉框里,但就是在显示的时候出现这个异常。不知道该怎么办了。请高手指点
      

  10.   

    这么说吧,我现在就是要把数据库里读出的一些数据,显示在那些文本框和下拉框里,还有没有别的方法。因为我要对那些用户进行修改,所以是必须显示在窗体上的。
    ==============
    这些没问题可以做到,主要是你要看看你该完代码后 控件生成的代码是否有误,例如你的控件没有添加到form的controls中
      

  11.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WinUser
    {
        public partial class AddUserFrm : Form
        {
            public AddUserFrm()
            {
                InitializeComponent();
                GetComBoxData();
                UserMessage userFrm = new UserMessage();
            }        /// <summary>
            /// 用构造函数传用户编号
            /// </summary>
            /// <param name="i"></param>
            public AddUserFrm(int i)
            {
                //int iRet = i;
                ReturnUserData(i);
            }        private void ReturnUserData(int iRet)
            {
                string strSQL = "";
                LinkDataBase link = new LinkDataBase();
                DataSet ds = new DataSet();
                DataTable dt;
                if (iRet != 0)
                {
                    strSQL = "select * from SysUser where SysUserID = '" + iRet + "'";
                }
                ds = link.SelectDB(strSQL);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                    this.textBox1.Text = dt.Rows[0]["SysUserCode"].ToString();
                    this.textBox2.Text = dt.Rows[0]["SysUserName"].ToString();
                    this.textBox3.Text = dt.Rows[0]["Psw"].ToString();
                    this.textBox4.Text = dt.Rows[0]["Email"].ToString();
                    this.textBox5.Text = dt.Rows[0]["Tel"].ToString();
                    this.textBox6.Text = dt.Rows[0]["Address"].ToString();
                    this.comboBox1.Text = dt.Rows[0]["DeptID"].ToString();
                    this.comboBox2.Text = dt.Rows[0]["Sex"].ToString();
                }
            }        #region 绑定部门下拉列表
            /// <summary>
            /// 绑定部门下拉列表
            /// </summary>
            private void GetComBoxData()
            {
                string strSQL = "";
                LinkDataBase link = new LinkDataBase();
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                strSQL = "select DeptID,DeptName from Dept where Dept.SupDeptID <> 0";
                ds = link.SelectDB(strSQL);
                dt = ds.Tables[0];
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember = dt.Columns["DeptName"].ToString();
                comboBox1.ValueMember = dt.Columns["DeptID"].ToString();
            }
            #endregion        private void toolStripButton1_Click(object sender, EventArgs e)
            {
                AddUserInfo();
            }        #region 添加用户
            /// <summary>
            /// 添加用户
            /// </summary>
            private void AddUserInfo()
            {
                string sSql = "";
                int iRet = 0;            #region
                string sID = textBox1.Text.ToString().Trim();
                string sName = textBox2.Text.ToString().Trim();
                string sPwd = textBox3.Text.ToString().Trim();
                string sEmail = textBox4.Text.ToString().Trim();
                string sTel = textBox5.Text.ToString().Trim();
                string sAddress = textBox6.Text.ToString().Trim();
                string sSex = comboBox2.Text.ToString().Trim();
                string sDept = comboBox1.Text.ToString().Trim();
                LinkDataBase link = new LinkDataBase();
                if (sID == "")
                {
                    MessageBox.Show("用户号不能为空!", "提示");
                    iRet = -1;
                }
                else if (sName == "")
                {
                    MessageBox.Show("用户名不能为空!", "提示");
                    iRet = -1;
                }
                else if (sPwd == "")
                {
                    MessageBox.Show("用户密码不能为空!", "提示");
                    iRet = -1;
                }
                else if (sDept == "")
                {
                    MessageBox.Show("请选择部门!", "提示");
                    iRet = -1;
                }
                else if (sSex == "")
                {
                    MessageBox.Show("请选择性别", "提示");
                    iRet = -1;
                }
                if (iRet == -1)
                {
                    return;
                }
                #endregion
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                //连表查询获取部门编号
                sSql = "select distinct SysUser.DeptID as DeptID  from SysUser inner join Dept on SysUser.DeptID = Dept.DeptID " +
                    "where Dept.DeptName = '" + sDept + "'";
                ds = link.SelectDB(sSql);
                long sDep = 0;
                //获取部门ID
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dt = ds.Tables[0];
                    sDep = long.Parse(dt.Rows[0]["DeptID"].ToString());
                    //int sDep = Convert.ToByte(dt.Rows[0]["DeptID"].ToString());
                }            sSql = "insert into SysUser(SysUserCode,SysUserName,Psw,Sex,Tel,Email,Address,DeptID) values('" + sID + "'" +
                    ",'" + sName + "','" + sPwd + "','" + sSex + "','" + sTel + "','" + sEmail + "','" + sAddress + "','" + sDep + "')";
                int i = link.InstUpdaDB(sSql);
                if (i == 1)
                {
                    //GetUserTable();
                    MessageBox.Show("用户添加成功!", "恭喜");
                    //this.GetUserListViewInfo();
                    this.ClearUserInfo();
                    return;
                }
                else
                {
                    MessageBox.Show("用户添加失败!", "遗憾");
                    this.ClearUserInfo();
                    return;
                }        }
            #endregion        #region 清楚文本框信息
            /// <summary>
            /// 清楚文本框信息
            /// </summary>
            private void ClearUserInfo()
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                textBox6.Text = "";
            }
            #endregion        private void toolStripButton2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
      

  12.   

    帮忙看看这个程序,异常就是出现在
    this.textBox1.Text = dt.Rows[0]["SysUserCode"].ToString();
                    this.textBox2.Text = dt.Rows[0]["SysUserName"].ToString();
                    this.textBox3.Text = dt.Rows[0]["Psw"].ToString();
                    this.textBox4.Text = dt.Rows[0]["Email"].ToString();
                    this.textBox5.Text = dt.Rows[0]["Tel"].ToString();
                    this.textBox6.Text = dt.Rows[0]["Address"].ToString();
                    this.comboBox1.Text = dt.Rows[0]["DeptID"].ToString();
                    this.comboBox2.Text = dt.Rows[0]["Sex"].ToString();其他都没错,请帮帮忙看看啊
      

  13.   

    哈哈,搞定了,原来是在构造函数时没加上InitializeComponent();这个。哈哈,真高兴,解决了