1

I'm trying to update records using C# ASP.NET. I have the following tables and textboxes.. The process would be to search for the EmployeeID first. Input the EmployeeID on txtID and hit btnSearch. The textboxes will be populated then by the values of EmployeeID = txtID.Text. txtLname, txtFname, etc will be populated. To save the changes, hit btnUpdate.

But whenever I try to click on the dropdownbox to change the department of the employee, there's no error but the change wasn't saved. Also, when I try to change the EmployeeID, it returns this error: Object reference not set to an instance of an object on this line dRow["EmployeeID"] = txtID.Text;.

EmployeeID and DeptID are both set as Primary Key


enter image description here enter image description here

Here's my code for 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();`

Here's my code for 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();

Here's the code for 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();

I just don't understand why this is happening. Please help me, I'm new to ASP.NET. Thanks.

4
  • Please post the code for your Page_Load event. Commented Nov 14, 2013 at 2:05
  • @KarlAnderson I added the Page_Load Commented Nov 14, 2013 at 2:10
  • Do you know how to debug your code in Visual Studio? Commented Nov 14, 2013 at 2:18
  • Upward for the good way of asking question. Commented Nov 14, 2013 at 5:20

1 Answer 1

1

This line here is the problem:

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

The issue is that the Text property of the text box named txtID is empty, thus the parsing of an empty string does not find a row in the database and dRow is null. So the first attempt to use dRow is on this line:

dRow["EmployeeID"] = txtID.Text;

And kaboom!

My recommendation is to put a guard clause in before you even try to find the row in the database, like this:

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.
}

UPDATE:

Your departments drop down is not updating, because on every page load (initial page load or post back), the code is re-binding the drop down before the code to save it to the database has run. In ASP.NET WebForms, the Page_Load happens before the click event handler (btnUpdate_click() in this case), so you are wiping out the choice made by the user and then trying to do the save.

In your Page_Load, try this instead:

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();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, maybe I should just disable the txtID so the value cannot be changed, since EmployeeID is Primary Key. How about for the drop down box? It's not updating.
If I change dRow["DeptID"] = drpDepartments.SelectedValue; to dRow["DeptID"] = drpDepartments.DataValueField; I'm getting this error Input string was not in a correct format.Couldn't store <DeptID> in DeptID Column. Expected type is Int32.
@user2971155 - see updated answer to put conditional logic in the Page_Load event.
@user2971155 - you want to keep using drpDepartments.SelectedValue, but read my updated answer to explain why SelectedValue is getting trashed from your code re-binding the drop down every post back to the server.
I tried it but still getting this error Object reference not set to an instance of an object. when I search for the EmployeeID. This line DataRow myRow = dsEmp.Tables["tblEmployee"].Rows.Find(int.Parse(txtID.Text));
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.