For Answers, see/post comments

DataTable Update

Hello, I have been working on this for a while and it is driving me insane, so any help would really be appreciated.

I send in to the below method a DataTable that has been edited through a dataGridView. I then get the original DataTable, set the original DataTable to equal the changed Datatable and then update the new Table. The problem I have is the Parameters I set in the BuildCommands method are always null.
Thanks for the help

public void updateDt(DataTable dt)
{
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataTable dtTempDataTable = new DataTable();
string SqlStmt = "SELECT * testId, testField FROM TEST";
try
{
string commandstring = SQLStmt;dataAdapter = new SqlDataAdapter(commandstring, DatabaseConnection); BuildCommands(dataAdapter);
dataAdapter.Fill(dtTempDataTable);
//fills with 4 rows dtTempDataTable = dt, //set new dt to dt with user changes dtTempDataTable.AcceptChanges();
dataAdapter.Update(dtTempDataTable); //doesnt do a thing
}
catch { }
finally { }
}

private void BuildCommands(SqlDataAdapter dataAdapter)
{
SqlConnection connection = (SqlConnection)dataAdapter.SelectCommand.Connection; SqlParameter workParam = null;
string query = "Update TEST Set testField = @testField WHERE testid= @testid";
dataAdapter.UpdateCommand = new SqlCommand(query, connection);
workParam = dataAdapter.UpdateCommand.Parameters.Add("@testField", SqlDbType.BigInt);

workParam.SourceColumn = "testfield";
workParam = dataAdapter.UpdateCommand.Parameters.Add("@testid", SqlDbType.BigInt); workParam.SourceColumn = "testId";
}

1 comment:

Anonymous said...

While I haven't worked with updated tables and the AcceptChanges method of the data adapter, I want to point out a basic problem with your code.

First you fill a data table variable:

dataAdapter.Fill(dtTempDataTable);//fills with 4 rows

Then you do the table assignment:

dtTempDataTable = dt, //set new dt to dt with user changes

This second line makes the prior line completely useless because you are overwriting the variable with the object that was passed into the method.

I think what you are trying to accomplish is a merge of the data. You are expecting that assignment to essentially merge the changed table into the loaded table in order to establish the rows that were changed. Unfortunately, this simply doesn't work that way.

From my understanding of the disconnected data model, you load a table, change the rows, then commit the changes using the AcceptChanges method. Without seeing the rest of the code in order to see how you are modifying the row values, etc, I can't provide you with more information.

The important point I'm trying to make is that the table variable assignment instruction is negating the previous loading of the table. Unless the table passed into the method (dt) contains rows that are in the 'modified' state, the AcceptChanges() method won't do anything.

I can't say for sure what the problem is with the parameters. Are the parameters null or are the parameter values null?

-Peter