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
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.
Page_Loadevent.Page_Load