Hello,
When l got to update my database l get this error message "Missing the datacolumn 'DepartmentName' in the DataTable 'Employee' for the sourceColumn 'DepartmentName' ".
I have a database application. 2 tables. Department and Employee.
Department -> DepartmentCode (PK)
DepartmentName
Employee -> EmployeeID (PK)
Name
Surname
DepartmentCode (FK)
I have create this relationship in code using relationships in the form load event. see below:
When l click the update button, l run this code and get an error on the update.Code:private void frmEmployees_Load(object sender, System.EventArgs e) { try { cnn.Open(); //Fill the DataSet with the Employee details OleDbCommand cmd = cnn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM Employee"; da.SelectCommand = cmd; //Gets all the details for the database, e.g. Primary keys da.FillSchema(ds,SchemaType.Source,"Employee"); da.Fill(ds,"Employee"); //Fill the DataSet wtih the Department details cmd.CommandText = "SELECT * FROM Department"; da.SelectCommand = cmd; da.FillSchema(ds,SchemaType.Source,"Department"); da.Fill(ds,"Department"); //Assign the parent column of the Department table and child column of the //employee table. Remember - DepartmentCode PK in Department table, and //FK in the Employee Table. DataColumn parentColumn = ds.Tables["Department"].Columns["DepartmentCode"]; DataColumn childColumn = ds.Tables["Employee"].Columns["DepartmentCode"]; //Clear the relation of any data. Not really nessessary as we are creating from new. //However, if we are to reuse then we must clear all contents. ds.Relations.Clear(); drEmployees = new DataRelation("EmployeeDetails",parentColumn,childColumn); ds.Relations.Add(drEmployees); //Display the details of the first employee in the database. txtEmployeeID.Text = ds.Tables["Employee"].Rows[0]["EmployeeNumber"].ToString(); txtFirstName.Text = ds.Tables["Employee"].Rows[0]["FirstName"].ToString(); txtSurname.Text = ds.Tables["Employee"].Rows[0]["Surname"].ToString(); txtDateOfBirth.Text = ds.Tables["Employee"].Rows[0]["DateOfBirth"].ToString(); txtAddress1.Text = ds.Tables["Employee"].Rows[0]["Address1"].ToString(); txtAddress2.Text = ds.Tables["Employee"].Rows[0]["Address2"].ToString(); cboDepartment.Text = ds.Tables["Employee"].Rows[0]["DepartmentCode"].ToString(); string departmentCode = ds.Tables["Employee"].Rows[0]["DepartmentCode"].ToString(); DataRow[] departmentRow = ds.Tables["Department"].Select("DepartmentCode = '" + departmentCode + "' "); string departmentName = departmentRow[0]["DepartmentName"].ToString(); this.UpdateDepartments();//Call to display combo box information } catch ( OleDbException ex ) { MessageBox.Show(ex.Message); } catch ( Exception ex ) { MessageBox.Show(ex.Message); } }
I hope someone can help me with this question. very important.Code:ds.Tables["Employee"].Rows[0]["FirstName"] = txtFirstName.Text; ds.Tables["Employee"].Rows[0]["Surname"] = txtSurname.Text; ds.Tables["Employee"].Rows[0]["DepartmentCode"] = departmentCode; da.Update(ds,"Employee"); //Error ocurrs here
Thanks in advance,
Steve




Reply With Quote