473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

data concurrency

Can anyone give me any advice on catching data concurrency errors? I
want to amend the code below so that if the record to be amended has
already changed since it was taken from the table, the error will be
trapped and the code will go into a loop where it continues trying to
update the value until it finds that the record has not been amended
since it was taken from the table, and so can successfully update.

SqlConnection objConnectionAttPurchLNX = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strPurpleCards = "SELECT * FROM purple_cards WHERE Serial =
" + Session["SerialNumber"];

//create a new DataSet object
DataSet objDataSetPurpleCards = new DataSet();

//create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapterPurpleCards = new
SqlDataAdapter(strPurpleCards, objConnectionAttPurchLNX);

//fill the dataset with data from the DataAdapter object
objDataAdapterPurpleCards.Fill(objDataSetPurpleCar ds,
"PurpleCards");

//declare a variable to reference the table
DataTable objTablePurpleCards =
objDataSetPurpleCards.Tables["PurpleCards"];

//edit an exisiting row in the table
DataRow objRow = null;
objRow = objTablePurpleCards.Rows[0];
objRow["Cash"] = Convert.ToDouble(Session["Cash"]) + dblTotal;

try
{
SqlCommand objCommandPurpleCards = new SqlCommand();
objCommandPurpleCards.Connection = objConnectionAttPurchLNX;
objCommandPurpleCards.CommandText = "UPDATE Purple_Cards SET Cash
= " + objRow["Cash"] + " WHERE Serial = " + Session["SerialNumber"];

objDataAdapterPurpleCards.UpdateCommand = objCommandPurpleCards;

// now do the update
objDataAdapterPurpleCards.Update(objDataSetPurpleC ards,
"PurpleCards");

//refresh data and update Session["Cash"]
SqlCommand objCommandPurpleCardsRefresh = new
SqlCommand(strPurpleCards, objConnectionAttPurchLNX);
SqlDataReader objDataReaderPurpleCardsRefresh = null;

objConnectionAttPurchLNX.Open();
objDataReaderPurpleCardsRefresh =
objCommandPurpleCardsRefresh.ExecuteReader();

if (objDataReaderPurpleCardsRefresh.Read() == true)
{
double dblCash;
dblCash =
Convert.ToDouble(objDataReaderPurpleCardsRefresh["CASH"]);
dblCash = Math.Round(dblCash, 2);
Session["Cash"] = dblCash;
lblBalance.Text = "Balance : £" + Session["Cash"];
}

}
catch (Exception objError)
{
lbl1.Text = Convert.ToString(objError);
}

objConnectionAttPurchLNX.Close();
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #1
1 3674
Mike,

You will have to have some sort of a timestamp on your table. When
updating your values, you will want to begin a transaction, checking to see
if the timestamp that you have and the timestamp on the server have changed.
If they have, then you don't do anything, otherwise, you save the values
(because the record hasn't changed).

I don't think that your algorithm is a good one, as you will find that
the timestamp that you have is always different than the original one.
Also, how do you mitigate the changes that were made, with the changes you
want to be made? What if a field was changed that you changed and that
write should overwrite the change you would have made? You need to be more
aware, and possibly make your users more aware of the concurrency issue and
then proceed based on user input, or through some very well-defined rules
for when this happens.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:uI**************@TK2MSFTNGP12.phx.gbl...
Can anyone give me any advice on catching data concurrency errors? I
want to amend the code below so that if the record to be amended has
already changed since it was taken from the table, the error will be
trapped and the code will go into a loop where it continues trying to
update the value until it finds that the record has not been amended
since it was taken from the table, and so can successfully update.

SqlConnection objConnectionAttPurchLNX = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTest"]);
string strPurpleCards = "SELECT * FROM purple_cards WHERE Serial =
" + Session["SerialNumber"];

//create a new DataSet object
DataSet objDataSetPurpleCards = new DataSet();

//create a new DataAdapter using the connection object and select
statement
SqlDataAdapter objDataAdapterPurpleCards = new
SqlDataAdapter(strPurpleCards, objConnectionAttPurchLNX);

//fill the dataset with data from the DataAdapter object
objDataAdapterPurpleCards.Fill(objDataSetPurpleCar ds,
"PurpleCards");

//declare a variable to reference the table
DataTable objTablePurpleCards =
objDataSetPurpleCards.Tables["PurpleCards"];

//edit an exisiting row in the table
DataRow objRow = null;
objRow = objTablePurpleCards.Rows[0];
objRow["Cash"] = Convert.ToDouble(Session["Cash"]) + dblTotal;

try
{
SqlCommand objCommandPurpleCards = new SqlCommand();
objCommandPurpleCards.Connection = objConnectionAttPurchLNX;
objCommandPurpleCards.CommandText = "UPDATE Purple_Cards SET Cash
= " + objRow["Cash"] + " WHERE Serial = " + Session["SerialNumber"];

objDataAdapterPurpleCards.UpdateCommand = objCommandPurpleCards;

// now do the update
objDataAdapterPurpleCards.Update(objDataSetPurpleC ards,
"PurpleCards");

//refresh data and update Session["Cash"]
SqlCommand objCommandPurpleCardsRefresh = new
SqlCommand(strPurpleCards, objConnectionAttPurchLNX);
SqlDataReader objDataReaderPurpleCardsRefresh = null;

objConnectionAttPurchLNX.Open();
objDataReaderPurpleCardsRefresh =
objCommandPurpleCardsRefresh.ExecuteReader();

if (objDataReaderPurpleCardsRefresh.Read() == true)
{
double dblCash;
dblCash =
Convert.ToDouble(objDataReaderPurpleCardsRefresh["CASH"]);
dblCash = Math.Round(dblCash, 2);
Session["Cash"] = dblCash;
lblBalance.Text = "Balance : £" + Session["Cash"];
}

}
catch (Exception objError)
{
lbl1.Text = Convert.ToString(objError);
}

objConnectionAttPurchLNX.Close();
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Suzanne | last post by:
Hi All I'm having problems getting my data adapter to throw a concurrency exception with an INSERT command. I want to throw a concurrency exception if an attempt is made to enter a row into...
3
by: Jimbo | last post by:
Ever since I moved the access 97 backend to a SQL Server backend with Access 97 front end..some forms pop up with a The data has been changed...Another user edited this record and saved the changes...
2
by: BobAchgill | last post by:
Do you know why this error might be happening? Maybe it is because I have two data adapters open on the same MDB file?? If this is the problem... how can I keep them from disturbing each other?...
34
by: Jeff | last post by:
For years I have been using VBA extensively for updating data to tables after processing. By this I mean if I had to do some intensive processing that resulted in data in temp tables, I would have...
4
by: Hardik Shah | last post by:
Hi, I have a combobox for city name help , which uses dataset as datasource , when other user from network add any other city or edit city, it is not reflect in my current form (in run mode) ,...
4
by: George | last post by:
Hi all, I am having trouble with updating my data in an Access database. here is my code: Imports System.Data.OleDb Dim AppPath As String = Mid(Application.ExecutablePath, 1,...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data, from a database. One user changes the data and...
3
by: John | last post by:
Hi I have a vs 2003 winform data app. All the data access code has been generated using the data adapter wizard and then pasted into the app. The problem I have is that I am getting a data...
8
by: Markus | last post by:
Hello everyone. Recently I stumbled upon an interesting problem related to thread-parallel programming in C (and similarily C++). As an example assume a simple "buffer" array of size 8, e.g....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.