Click here to Skip to main content
16,016,759 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an issue with updating two different table which are in same and also in different database.
When I use the following code, it is not updating second table and saying
concurrency violation the updatecommand affected 0 of the expected error
Please note in following code I am updating Grid one column and updating the Database.

"mySymbolMastertable" table is already populated with changes.
PLEASE HELP...
C#
private void 

UpdateDatabases() 

{

strAddResult =new StringBuilder (); 

try

{

DataTable dtsec= new DataTable () ; 

dtsec= mySymbolMastertable.GetChanges();

 if (connList != null ) 

{ 

for (int i = 0; i < connList.Count; i++) 

{

 string connectionName; 

 string sqlString; 

 DataTable dt = new DataTable 

(); 

connectionName = connList[i].ToString();

connectionString = System.Configuration.ConfigurationSettings .AppSettings[connectionName]; 

 SqlConnection mySQLConnection = new SqlConnection (connectionString);  

 if (connectionName == "SecMasterConnectionSec" ) 

{

sqlString = "SELECT * FROM [SymbolMaster_Secondary]" ; 

}

 else 

{

sqlString ="SELECT * FROM [SymbolMaster]" ; 

} 

 SqlDataAdapter myDataAdapter = new SqlDataAdapter (sqlString, mySQLConnection); 

 SqlCommandBuilder myCmdBuilder = new SqlCommandBuilder (myDataAdapter); 

 dt = dtsec; 

 // dt = mySymbolMastertable.GetChanges();  

 //Add Handler

 //myDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating);

 myDataAdapter.RowUpdated +=new SqlRowUpdatedEventHandler (myDataAdapter_RowUpdated); 

myDataAdapter.ContinueUpdateOnError =true ; 

 //myDataAdapter.InsertCommand = myCmdBuilder.GetInsertCommand();

 //myDataAdapter.UpdateCommand = myCmdBuilder.GetUpdateCommand();

 myDataAdapter.Update(dt);

 //Add Handler

 // myDataAdapter.RowUpdating += new SqlRowUpdatingEventHandler(myDataAdapter_RowUpdating);

myDataAdapter.RowUpdated +=new SqlRowUpdatedEventHandler (myDataAdapter_RowUpdated); 

 myCmdBuilder =null ; 

myDataAdapter =null; 

mySQLConnection =null ; 

 }}

} 

catch (Exception ex) 

{  

''Catching Exception 

}}

PLEASE HELP...
Posted
Updated 15-Nov-11 1:37am
v2

When we are using CommandBuilder and using update method of DataAdapter so connection will automatically open and close.

As I said, it is updating first time so I think connection is ok.

Please let me know if you find something else
 
Share this answer
 
When you call GetChanges(), you get a new copy of main table's changed rows(mySymbolMastertable) .Every GetChanges call returns the same table(dtSec) unless you change mySymbolMastertable.Assigning dtSec to dt does nothing.You update still dtSec.Maybe you can call dtSec.Copy() to not to update dtSec for next Update call.Has getting GetChanges() in your for loop solved your problem?
 
Share this answer
 
v3
This occurs when you try to update a row in a table(dtsec in your case) but that row doesn't exist in original database.Simply if another procedure delete a row or change a row from database,you can't update that row since the row you hold isn't matching with db..Be sure that all row in dtsec exist in second database.You can read Ado.Net Optimistic Concurrency model.When updating a row using optimistic concurrency ,the value you have has to match value in database (What I mean by value is all columns in a row ,cell values)
 
Share this answer
 
If you see, mySymbolMastertable is global variable which is carrying grid values. mySymbolMastertable datatbale table has changed value. I am looping throug connection which we have in config file. Every loop, I am creating a new dataset (dtSec = new Dataset();) and calling mySymbolMastertable.GetChanges() function.

dtsec= mySymbolMastertable.GetChanges();

and assigning dt-dtsec (Not neccssary but doing)

In every loop, I am creating new instanciate of DataAdapter, connection and commandbuilder.

Dataadapter event raised first time for one table but for second table either it is not raised or giving concurrency error.
 
Share this answer
 
I can't find where you call mySQLConnectio.Open()?
 
Share this answer
 
After you call myDataAdapter.Update() dtsec's rows become unchanged so for second table, there is nothing to update..Get
C#
DataTable dtsec= new DataTable () ; 
 
dtsec= mySymbolMastertable.GetChanges(); 

lines in for loop.
 
Share this answer
 
v2
So if we call GetChanges(); method and get changes in new Datatable so next time If we call GetChanges(); so we will get nothing?

If you see every time, I am getting GetChage in new Datatable (dt) and passing that new table (dt) to dataadapter.update method.

Please correct if I m wrong.

Thanks again...
 
Share this answer
 
Thanks..doing dtSec.Copy() solve the problem.

Is any way I can Use AcceptChanges and GetChanges funtion?

Copy function copy all the data so it is bit slow copare to GetChanges().
 
Share this answer
 
Calling AcceptChanges() on datatable after update Solve issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900