Results 1 to 7 of 7

Thread: updating a dataset

  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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating a dataset

    What's the SQL for your update statement?

  3. #3

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

    Re: updating a dataset

    Hello,

    I am not using a sql string, but updating uisng the command builder and using the data adapter to update the back-end database.

    I have never counted a problem like this before, and l have done this many time. I am not sure, but l think it could have something to do with the ralationship l have created in the load event of the form.

    Any ideas, l would be most grateful to you.

    VB Code:
    1. private void btnUpdateEmployee_Click(object sender, System.EventArgs e)
    2.         {
    3.  
    4.             try
    5.             {
    6.                 OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
    7.  
    8.                
    9.        
    10.                 ds.Tables["Employee"].Rows[0]["FirstName"] = txtFirstName.Text;
    11.                 ds.Tables["Employee"].Rows[0]["Surname"] = txtSurname.Text;
    12.                 ds.Tables["Employee"].Rows[0]["DepartmentCode"] = txtdepartmentCode.Text;
    13.  
    14.                 da.Update(ds,"Employee");
    15.             }
    16.             catch ( OleDbException ex )
    17.             {
    18.                 MessageBox.Show(ex.Message);
    19.             }
    20.             catch ( Exception ex )
    21.             {
    22.                 MessageBox.Show(ex.Message);
    23.             }
    24.         }

    Thanks in advance,

    Steve
    Last edited by steve_rm; Jun 26th, 2005 at 10:26 AM.
    steve

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: updating a dataset

    Are you using the same DataTable that you filled originally or have you created a new DataSet altogether? If you're using a CommandBuilder then it will make certain assumptions based on the SQL query you performed. If you selected all columns then it will try to update all columns. If your DataSet and/or DataTable is the same object that you filled in the first place, I wouldn't have thought that this would be a problem because the DepartmentName column should be present. It sounds like the update SQL is trying to update that column but it is not present in the DataTable. If for some reason you don't want to update that column then you should be writing your own update SQL and not using a command builder.

    If you are unsure of how to proceed, you could use a breakpoint at the call to Update and use the Watch window to examine the columns in the DataTable and the SQL commands. The command builder has methods to access the actual commands being used. Alternatively, you could use Debug.WriteLine to write certain values of interest to the Output window.

  5. #5

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

    Re: updating a dataset

    Thanks for you reply,

    I will follow up with what you have said.

    I will let you know about my findings.

    Thanks,

    Steve
    steve

  6. #6
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: updating a dataset

    have you tried putting ds.tables["Employees"].getchanges()?

  7. #7

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

    Re: updating a dataset

    Hello,

    I managed to solve the problem, but l am not sure why this would work. I have inserted code to refill the dataset with the employee information. But l can't understand, why l have do do this again when i have filled it when the form loads.

    See my code below.

    VB Code:
    1. private void btnUpdateEmployee_Click(object sender, System.EventArgs e)
    2.         {
    3.             OleDbCommand cmd = cnn.CreateCommand();
    4.             cmd.CommandType = CommandType.Text;
    5.             cmd.CommandText = "SELECT * FROM Employee";
    6.  
    7.             da.SelectCommand = cmd;
    8.             da.Fill(ds,"Employee");
    9.  
    10.             try
    11.             {
    12.                 OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
    13.  
    14.                
    15.        
    16.                 ds.Tables["Employee"].Rows[0]["FirstName"] = txtFirstName.Text;
    17.                 ds.Tables["Employee"].Rows[0]["Surname"] = txtSurname.Text;
    18.                 ds.Tables["Employee"].Rows[0]["DepartmentCode"] = departmentCode;
    19.                
    20.                 da.Update(ds,"Employee");
    21.             }
    22.             catch ( OleDbException ex )
    23.             {
    24.                 MessageBox.Show(ex.Message);
    25.             }
    26.             catch ( Exception ex )
    27.             {
    28.                 MessageBox.Show(ex.Message);
    29.             }
    30.         }

    If you know the answer, please let me know.

    Thanks in advance,

    Steve
    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