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();就出现“未将对象引用设置到对象的实例”啊,请帮帮,急呀!
{
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();就出现“未将对象引用设置到对象的实例”啊,请帮帮,急呀!
不存在SysUserCode列??
如果是这样 string s = dt.Rows[0]["SysUserCode"].ToString();就不会有错,请高手指点。非常感谢。
=====
调试
看看this.textBox1是否为空
Respone.Write( this.Text==null )
======================
是不是你手动修改过vs.net 自动生成的代码??
==============
这些没问题可以做到,主要是你要看看你该完代码后 控件生成的代码是否有误,例如你的控件没有添加到form的controls中
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();
}
}
}
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();其他都没错,请帮帮忙看看啊