1

我正在尝试使用 C# ASP.NET 更新记录。我有以下表格和文本框。该过程将首先搜索 EmployeeID。输入EmployeeIDon txtID 并点击 btnSearch。文本框将由 的值填充EmployeeID = txtID.TexttxtLname, txtFname, 等将被填充。要保存更改,请点击btnUpdate

但是每当我尝试单击下拉框来更改员工的部门时,都没有错误,但没有保存更改。此外,当我尝试更改 EmployeeID 时,它会返回此错误:Object reference not set to an instance of an objecton this line dRow["EmployeeID"] = txtID.Text;

EmployeeID 和 DeptID 都设置为主键


在此处输入图像描述 在此处输入图像描述

这是我的 btnSearch_Click() 代码

        DataRow myRow = dsEmp.Tables["tblEmployee"].Rows.Find(int.Parse(txtID.Text));

        txtID.Text = myRow["EmployeeID"].ToString();
        txtLname.Text = myRow["Lname"].ToString();
        txtFname.Text = myRow["Fname"].ToString();
        txtMname.Text = myRow["Mname"].ToString();
        txtAddress.Text = myRow["Address"].ToString();
        txtEmail.Text = myRow["Email"].ToString();
        txtPhone.Text = myRow["Phone"].ToString();
        txtJobtitle.Text = myRow["Jobtitle"].ToString();
        txtSalary.Text = myRow["Salary"].ToString();
        drpDepartments.SelectedValue = myRow["DeptID"].ToString();`

这是我的 btnUpdate_click() 代码

        cb = new SqlCommandBuilder(daEmp);

        DataRow dRow = dsEmp.Tables["tblEmployee"].Rows.Find(int.Parse(txtID.Text));
        dRow["EmployeeID"] = txtID.Text;
        dRow["DeptID"] = drpDepartments.SelectedValue;
        dRow["Lname"] = txtLname.Text;
        dRow["Fname"] = txtFname.Text;
        dRow["Mname"] = txtMname.Text;
        dRow["Address"] = txtAddress.Text;
        dRow["Email"] = txtEmail.Text;
        dRow["Phone"] = txtPhone.Text;
        dRow["Jobtitle"] = txtJobtitle.Text;
        dRow["Salary"] = txtSalary.Text;

        daEmp.Update(dsEmp, "tblEmployee");
        dsEmp.Tables["tblEmployee"].AcceptChanges();

这是 Page_Load 的代码

            sConn = new SqlConnection(sStr);
        daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
        daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
        dsEmp = new DataSet();
        dsDep = new DataSet();

        daEmp.Fill(dsEmp, "tblEmployee");
        daDep.Fill(dsDep, "tblDepartment");

        dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

        drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
        drpDepartments.DataTextField = "Description";
        drpDepartments.DataValueField = "DeptID";
        drpDepartments.DataBind();

我只是不明白为什么会这样。请帮助我,我是 ASP.NET 的新手。谢谢。

4

1 回答 1

1

这行是问题所在:

DataRow dRow = dsEmp.Tables["tblEmployee"].Rows.Find(int.Parse(txtID.Text));

问题是Text名为的文本框的属性txtID是空的,因此空字符串的解析在数据库中没有找到一行并且dRownull. 所以第一次尝试使用dRow就在这一行:

dRow["EmployeeID"] = txtID.Text;

和kaboom!

我的建议是在您尝试在数据库中查找行之前添加一个保护子句,如下所示:

if(!String.IsNullOrEmpty(txtId.Text))
{
    // Yes, the text box has a value so try to look it up
    DataRow dRow = dsEmp.Tables["tblEmployee"].Rows.Find(int.Parse(txtID.Text));

    // Is dRow null?
    if(dRow != null)
    {
        dRow["EmployeeID"] = txtID.Text;
        dRow["DeptID"] = drpDepartments.SelectedValue;
        dRow["Lname"] = txtLname.Text;
        dRow["Fname"] = txtFname.Text;
        dRow["Mname"] = txtMname.Text;
        dRow["Address"] = txtAddress.Text;
        dRow["Email"] = txtEmail.Text;
        dRow["Phone"] = txtPhone.Text;
        dRow["Jobtitle"] = txtJobtitle.Text;
        dRow["Salary"] = txtSalary.Text;
    }
}
else
{
    // Unable to find an empty string value in the database so don't even try
    // You can display an error message maybe or throw an exception, etc.
}

更新:

您的部门下拉列表不会更新,因为在每次页面加载(初始页面加载或回发)时,代码都会在将其保存到数据库的代码运行之前重新绑定下拉列表。在 ASP.NET WebForms 中,Page_Load发生在单击事件处理程序之前(btnUpdate_click()在本例中),因此您正在清除用户所做的选择,然后尝试进行保存。

在你的Page_Load,试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        // Do not do these things on every post back to the server
        sConn = new SqlConnection(sStr);
        daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
        daDep = new SqlDataAdapter("SELECT * FROM tblDepartment", sConn);
        dsEmp = new DataSet();
        dsDep = new DataSet();

        daEmp.Fill(dsEmp, "tblEmployee");
        daDep.Fill(dsDep, "tblDepartment");

        dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

        drpDepartments.DataSource = dsDep.Tables["tblDepartment"];
        drpDepartments.DataTextField = "Description";
        drpDepartments.DataValueField = "DeptID";
        drpDepartments.DataBind();
    }
}
于 2013-11-14T02:14:20.600 回答