Question

I have a C# ASP.net project that sends a dataset to my connection class where I update the database accordingly.

The dataset I send through is populated with data from 2 tables from my database, thus I used a Join to get the data. (thus the individual update)

Now I have made the changes I want to the dataset and want to Update the database. Both section 1 and 2 of my code works IF only one is run at a time (thus either section 1 or 2 should be commented out).

But when I try and run both, it only updates the database with the first part (no error is thrown, and the code does execute)

Why does this happen? I've also closed and re-opened my connection after the first update to see if that made any difference.

   public void udpateCourse(DataSet dataSetEmp)
    {
        try
        {
            conn.Open();
            //SECTION 1 -- THE FIRST UPDATE
            da = new SqlDataAdapter("select * from EthicsManagement", conn);
            var builderForTable1 = new SqlCommandBuilder(da);
            da.Update(dataSetEmp, "Table");

            //SECTION 2 -- THE SECOND UPDATE
            da = new SqlDataAdapter("select employeeId, name as [Employee Name] from EmployeeTable", conn);
            builderForTable1 = new SqlCommandBuilder(da);
            da.Update(dataSetEmp, "Table");

            conn.Close();
        }
        catch (Exception ex)
        {
            throw new Exception("Problem with the SQL connection " + ex);
        }
    }

--Update-- What I've Tried

  1. Closing and opening the connection again
  2. New Instances of the adapter and builders
  3. even putting them in a separate method
  4. Having one query (a join on two tables)
Was it helpful?

Solution

I know it's an old question, but perhaps it helps someone.

MSDN says the following happens when calling Update:

When using Update, the order of execution is as follows:

  1. The values in the DataRow are moved to the parameter values.
  2. The OnRowUpdating event is raised.
  3. The command executes.
  4. If the command is set to FirstReturnedRecord, then the first returned result is placed in the DataRow.
  5. If there are output parameters, they are placed in the DataRow.
  6. The OnRowUpdated event is raised.
  7. AcceptChanges is called.

The second Update() doesn't have any effect, because the changes to it were committed by the AcceptChanges() command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top