Results 1 to 7 of 7

Thread: updating a dataset

Threaded View

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    updating a dataset

    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:
    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);
    			}
    		}
    When l click the update button, l run this code and get an error on the update.

    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
    I hope someone can help me with this question. very important.

    Thanks in advance,

    Steve
    Last edited by steve_rm; Jun 25th, 2005 at 07:00 AM.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width