无法从下拉框更新值

时间:2013-11-14 01:37:45

标签: c# asp.net

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

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

EmployeeID和DeptID都设置为主键


enter image description here enter image description here

这是我的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的新手。感谢。

1 个答案:

答案 0 :(得分:1)

这一行是问题所在:

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

问题是名为Text的文本框的txtID属性为空,因此解析空字符串时在数据库中找不到行,dRow为{ {1}}。因此,第一次使用null的尝试就在这一行:

dRow

和kaboom!

我的建议是在你试图找到数据库中的行之前放入一个保护条款,如下所示:

dRow["EmployeeID"] = txtID.Text;

更新:

您的部门下拉不会更新,因为在每个页面加载(初始页面加载或回发)时,代码将重新绑定下拉列表,然后将代码保存到数据库中。在ASP.NET WebForms中,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. } 发生在单击事件处理程序(在本例中为Page_Load)之前,因此您将消除用户做出的选择,然后尝试执行保存。

btnUpdate_click()中,请尝试以下方式:

Page_Load