Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hey guys,

I have made an application that uses sql server to keep data and retrieve it when needed. Today me and my friend tried
to update data of same record from different computers, it threw exception. I have read a lot about 'Optimistic concurrency' but to be honest, I don't know how to implement it. Can anyone please help me or atleast guide me
on how should i implement it.

Thanks and regards
ajinkya
Posted
Comments
R. Giskard Reventlov 8-Apr-11 3:22am    
What was the exception?

Most data 'layers' have some kind of check to see if the record is still the same by the time you commit changes to the database.
For example, you retrieve a record from the database containing 'a', and you change it to 'b'. When you commit that to the database, the command you execute will check for 'a' and update that to 'b'. Now if somebody in the meantime changes the 'a' in the database to a different value (say 'c', the command check for 'a' and change it to 'b' will result in nothing, because 'a' doesn't exist anymore (because it's already changed to 'c').

Your code 'knows' there's a change an expects your database to update 1 record, but the database returns a result of 0 changed records. That is exactly what your error means.

You need to find a way to handle that kind of changes depending on some choises. You can choose for example to retrieve the record from the database, update the user's changes and immidiately write it back to the database, but there's a chance you'll undo the changes made in the first transaction.

You may want to take a peek at this[^] page. It explains way better what's going on than I can.

Good luck!

Eduard
 
Share this answer
 
v2
Comments
ajinkya11121 8-Apr-11 6:11am    
thanks a ton for your reply.
Link you posted really helped me. :)
Eduard Keilholz 8-Apr-11 6:24am    
Thanks, if you've found your answer you may want to accept one of the given answers by clicking the 'Accept Solution' button to indicate your problem is solved so nobody spends a lot of time answering your question when the problem is already solved.
 
Share this answer
 
Comments
Eduard Keilholz 8-Apr-11 4:05am    
Good resources Sandeep! +5
Sandeep Mewara 8-Apr-11 6:17am    
Thanks Eduard.
ajinkya11121 8-Apr-11 6:11am    
Hey sandeep,
Thanks a lot man. These links are indeed very helpful.
Sandeep Mewara 8-Apr-11 6:17am    
Glad it helps.
string ConStr = ConfigurationSettings.AppSettings["DATA_BASE"];
private void control_click(object sender...)
{
SqlConnection con = new SqlConnection(ConStr);
stirng Sql="UPDATE TBL_NAME SET (COLUMN) VALUES (@PARAMETER) <WHERE CONDITION IF REQUIRED>";
SqlCommand cmd = new SqlCommand(Sql, con);

cmd.Paramenter.AddWithValue("@PARAMENTER", TXT.text);

try
{
cmd.ExecuteNonQuery();
MessageBox("Updated Successfully");
}
catch
{
MessageBox("Error occured while updating");
}
}
 
Share this answer
 
string ConStr = ConfigurationSettings.AppSettings["DATA_BASE"];<br />
private void control_click(object sender...)<br />
{<br />
  SqlConnection con = new SqlConnection(ConStr);<br />
  stirng Sql="UPDATE TBL_NAME SET (COLUMN) VALUES (@PARAMETER) <WHERE CONDITION IF REQUIRED>";<br />
  SqlCommand cmd = new SqlCommand(Sql, con);<br />
  cmd.Paramenter.AddWithValue("@PARAMENTER", TXT.text);<br />
  try<br />
  {<br />
     cmd.ExecuteNonQuery();<br />
     MessageBox("Updated Successfully");<br />
  }<br />
  catch<br />
  {<br />
     MessageBox("Error occured while updating");<br />
  }<br />
}
 
Share this answer
 
Comments
ajinkya11121 8-Apr-11 6:10am    
hey thanks for your reply.
i am trying to include this in my code.
can you tell me what '@PARAMETER' stands for?

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