473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Concurrency error handling

Hi

I have asked this question before but have not received any clear answer. I
have a winform app with bound controls via dataadapter/dataset combination.I
have a dbconcurrency exception ex. Now I have the following;

DataRow = ex.Row
DataTable = ex.Row.Table
DataColumns = ex.Row.Table.Co lumns
DataSet = ex.row.Table.Da taset

How can I now via code, do the following;

1. Make a copy of the datarow,

2. Re-read the row from datasource,

3. Update read row with values form row copy.

Any help woudl be appreciated.

Many Thanks

Regards

Jul 18 '08 #1
7 1810
Hi John,

What you need to do is to create a selectcommand, insertcommand,
updatecommand with your dataAdapter. Note: the DataAdapter does not
work with MS Access - well except for the Select command, but none of
the action commands like Insert/Update/Delete. But they work well with
MS Sql Server (and probably Oracle)

Imports System
Imports System.Data.Sql Client
...

'--form level variables here
Dim da as New SqlDataAdapter, ds As New Dataset
Dim conn As New SqlConnection

Private Sub Form1_Load(...)
conn.Connection String = "Data Source=yourSvr; Initial
Catalog=yourDB; Integrated Security=True"

da.SelectComman d = New SqlCommand
da.InsertComman d = New SqlCommand
da.UpdateComman d = New SqlCommand

da.SelectComman d.connection = conn
da.InsertComman d.connection = conn
da.UpdateComman d.connection = conn

da.SelectComman d..CommandText = "Select * From tblx Where..."
da.InsertComman d.CommandText = "Insert Into tblx Select @f1, @f2 @f3"

da.UpdateComman d.CommandText = "Update tblx Set f1 = @f1, f2 = @f2, f3 =
@f3 Where ID = @ID"

da.InsertComman d.Parameters.Ad d("@f1", SqlDBType.Varch ar, 50, "f1")
...
da.UpdateComman d.Parameters.Ad d("@f1", SqlDbType.Varch ar, 50, "f1")

da.Fill(ds, "tbl_Local" )

End Sub

Private Sub Insert_Update()

Dim dr As DataRow
dr = ds.Tables("tbl_ Local").NewRow
dr.BeginEdit
dr("f1") = "test1"
dr("f2") = "test2"
dr("f3") = "test3
dr.EndEdit
ds.Tables("tbl_ Local").Rows.Ad d(dr)
da.Update(ds, "tbl_Local" )

End Sub

So you create tbl_Local on the fly with the da.Fill command. Then
whenever you add a new row to tbl_Local or edit a field in one of the
rows - the dataAdapter da will automatically know what happened. You
call the da.Update command -- for Inserts and Update (and deletes also).
This will update the table on the server with either a new row or an
edit update on a particular row. tbl_local is a mirror image of the
server table in your local memory.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Jul 18 '08 #2
John,

All your three questions are impossible to do.
>
1. Make a copy of the datarow,
A datarow is a collection of objects that exist partially in the datarow
itself as item collection and partially in the datatable columns collection.
2. Re-read the row from datasource,
You never need to re-read the row from a datasource as you haven't
overwritten it.

3. Update read row with values form row copy.
As long as you don't reset your concurrency protection, you would not be
able to overwrite it with whatever you want, but if you want to do that, you
simple use the original row and disable the concurrency checking.

Cor

Jul 20 '08 #3
Hi Cor

Thanks, How can I disable concurrency checking?

Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:uc******** ******@TK2MSFTN GP02.phx.gbl...
John,

All your three questions are impossible to do.
>>
1. Make a copy of the datarow,
A datarow is a collection of objects that exist partially in the datarow
itself as item collection and partially in the datatable columns
collection.
>2. Re-read the row from datasource,
You never need to re-read the row from a datasource as you haven't
overwritten it.

>3. Update read row with values form row copy.
As long as you don't reset your concurrency protection, you would not be
able to overwrite it with whatever you want, but if you want to do that,
you simple use the original row and disable the concurrency checking.

Cor

Jul 20 '08 #4
John wrote:
Hi

I have asked this question before but have not received any clear
answer. I have a winform app with bound controls via
dataadapter/dataset combination.I have a dbconcurrency exception ex.
Now I have the following;
DataRow = ex.Row
DataTable = ex.Row.Table
DataColumns = ex.Row.Table.Co lumns
DataSet = ex.row.Table.Da taset

How can I now via code, do the following;

1. Make a copy of the datarow,

2. Re-read the row from datasource,

3. Update read row with values form row copy.

Any help woudl be appreciated.

Many Thanks

Regards
This sounds to me like you want to make the changes, despite encountering a
concurrency exception. If that is correct, then why are you checking concurrency
in the first place? It is not required; it is something you decide to do if you
need to block an update when the row on the server has changed. If you don't
want to block such an update, then don't check it.

What is the purpose of the copy of the datarow? Whether you use it or the
original to update the "read row" would make no difference at all. Either way,
when you are done, the "read row" will now match both the original row and the
copy; what actually is the point of that?

I have a suspicion that you want to identify which columns the user has changed,
and change those, leaving the remaining columns as they exist on the server. If
that is the case, option one would be to only update changed columns in the
first place, instead of all columns. The other option is to re-read the row on
the server, then identify the changes made by a user and apply those to the
newly read row. That would work as long as the row doesn't get changed on the
server again in the mean time.

You can examine a row to see what has been changed, by looking at the Original
and Current values of each column. Here is an example. Perhaps thinking about
this will help you define what you are trying to do, and help you find a way to
do it.

Private Sub TestRow(ByVal DR As DataRow)
Dim Curr As Object
Dim Orig As Object
Dim dc As DataColumn
For Each dc In DR.Table.Column s
Curr = DR.Item(dc, DataRowVersion. Current)
Orig = DR.Item(dc, DataRowVersion. Original)
If Curr.Equals(Ori g) Then
' user has not changed it
Else
' user changed this column
End If
Next
End Sub


Jul 20 '08 #5
Hi John,

I thought that I had answered you, however I don't see my answer

:-)

Have a look at the SQL code for the update in your program or stored
procedures where is written something as.

Select TimeStamp from Table

If TimeStamp = @TimeStamp
Begin and then the update transaction in SQL .

As you delete that part, then the SQL code will probably eat everything you
deliver to it.

(It can also be a range of old values, compared with the new selected
existing values)

(It is probably easier to write the Update SQL yourself new by hand and than
skip the Select of the current values or current tinestamp)

Cor
"John" <in**@nospam.in fovis.co.ukschr eef in bericht
news:u0******** ******@TK2MSFTN GP05.phx.gbl...
Hi Cor

Thanks, How can I disable concurrency checking?

Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:uc******** ******@TK2MSFTN GP02.phx.gbl...
>John,

All your three questions are impossible to do.
>>>
1. Make a copy of the datarow,
A datarow is a collection of objects that exist partially in the datarow
itself as item collection and partially in the datatable columns
collection.
>>2. Re-read the row from datasource,
You never need to re-read the row from a datasource as you haven't
overwritten it.

>>3. Update read row with values form row copy.
As long as you don't reset your concurrency protection, you would not be
able to overwrite it with whatever you want, but if you want to do that,
you simple use the original row and disable the concurrency checking.

Cor

Jul 22 '08 #6
Hi Cor

I have the below Update SP (slightly simplified to exclude irrelevant
columns);

UPDATE [dbo].[Clients] SET [Company] = @Company, [Address 1] = @Address_1
WHERE (([ID] = @Original_ID) AND ([SSMA_TimeStamp] =
@Original_SSMA_ TimeStamp));
SELECT ID, Company, [Address 1] SSMA_TimeStamp FROM Clients WHERE (ID = @ID)

Ideally on concurrency violation I would like to give user option to force
save or cancel save. What would be the mechanism for that? My applications
is a vb.net winform app.

Many Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:0D******** *************** ***********@mic rosoft.com...
Hi John,

I thought that I had answered you, however I don't see my answer

:-)

Have a look at the SQL code for the update in your program or stored
procedures where is written something as.

Select TimeStamp from Table

If TimeStamp = @TimeStamp
Begin and then the update transaction in SQL .

As you delete that part, then the SQL code will probably eat everything
you
deliver to it.

(It can also be a range of old values, compared with the new selected
existing values)

(It is probably easier to write the Update SQL yourself new by hand and
than
skip the Select of the current values or current tinestamp)

Cor
"John" <in**@nospam.in fovis.co.ukschr eef in bericht
news:u0******** ******@TK2MSFTN GP05.phx.gbl...
>Hi Cor

Thanks, How can I disable concurrency checking?

Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:uc******* *******@TK2MSFT NGP02.phx.gbl.. .
>>John,

All your three questions are impossible to do.
1. Make a copy of the datarow,

A datarow is a collection of objects that exist partially in the datarow
itself as item collection and partially in the datatable columns
collection.

2. Re-read the row from datasource,

You never need to re-read the row from a datasource as you haven't
overwritten it.
3. Update read row with values form row copy.

As long as you don't reset your concurrency protection, you would not be
able to overwrite it with whatever you want, but if you want to do that,
you simple use the original row and disable the concurrency checking.

Cor


Jul 26 '08 #7
John,

Then create an extra Update where that WHERE clause part from the timestamp
from the one you show now is deleted.

I can not tell you how to do it in VB as I don't know what you use to update
in that, it can be an execute nonquery or a xxAdapter. Those you have of
course to make new, or simple set the right update sql to its command every
time you use the original or the overwritting one.
However be aware what you doing, as this can give big errors.
(There is a sample in banking busines where this was used to fraud).

Cor

"John" <in**@nospam.in fovis.co.ukschr eef in bericht
news:u5******** *****@TK2MSFTNG P02.phx.gbl...
Hi Cor

I have the below Update SP (slightly simplified to exclude irrelevant
columns);

UPDATE [dbo].[Clients] SET [Company] = @Company, [Address 1] = @Address_1
WHERE (([ID] = @Original_ID) AND ([SSMA_TimeStamp] =
@Original_SSMA_ TimeStamp));
SELECT ID, Company, [Address 1] SSMA_TimeStamp FROM Clients WHERE (ID =
@ID)

Ideally on concurrency violation I would like to give user option to force
save or cancel save. What would be the mechanism for that? My applications
is a vb.net winform app.

Many Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:0D******** *************** ***********@mic rosoft.com...
>Hi John,

I thought that I had answered you, however I don't see my answer

:-)

Have a look at the SQL code for the update in your program or stored
procedures where is written something as.

Select TimeStamp from Table

If TimeStamp = @TimeStamp
Begin and then the update transaction in SQL .

As you delete that part, then the SQL code will probably eat everything
you
deliver to it.

(It can also be a range of old values, compared with the new selected
existing values)

(It is probably easier to write the Update SQL yourself new by hand and
than
skip the Select of the current values or current tinestamp)

Cor
"John" <in**@nospam.in fovis.co.ukschr eef in bericht
news:u0******* *******@TK2MSFT NGP05.phx.gbl.. .
>>Hi Cor

Thanks, How can I disable concurrency checking?

Thanks

Regards

"Cor Ligthert[MVP]" <no************ @planet.nlwrote in message
news:uc****** ********@TK2MSF TNGP02.phx.gbl. ..
John,

All your three questions are impossible to do.

>
1. Make a copy of the datarow,
>
A datarow is a collection of objects that exist partially in the
datarow itself as item collection and partially in the datatable
columns collection.

2. Re-read the row from datasource,
>
You never need to re-read the row from a datasource as you haven't
overwritte n it.
3. Update read row with values form row copy.
>
As long as you don't reset your concurrency protection, you would not
be able to overwrite it with whatever you want, but if you want to do
that, you simple use the original row and disable the concurrency
checking.

Cor


Jul 26 '08 #8

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

Similar topics

17
2171
by: David Rasmussen | last post by:
Are there any indicators for 1) whether the C++ commitee will include concurrency in the next standard 2) what it will look like (Java, ZThreads, etc.) /David
2
5122
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data store. I've done a fair amount of research on concurrency handling in newsgroups and other resources. Below is what I've come up as a standard for handling concurrency thru stored procedures. I am sharing with everyone so I can get some comments...
2
4718
by: Mullin Yu | last post by:
I create a component to write the error log as follow, but it seems sometimes locked by other processes, and pop up the I/O exception. How can we handling the concurrency issue at file level? Retry accessing the file if catch error => like waiting and polling. Thanks! private void LogError2File(string errorMessage, string LogFilePath)
3
2500
by: Robert Schuldenfrei | last post by:
Hi NG, I am looking for an opinion here. I am new to C# and SQL, being an old COBOL hand. I have started into a conversion of an old COBOL ERP system. I have a number of functions working now and it is time to decide how best to deal with the issues of concurrency. The old COBOL programs used record locking to prevent concurrency errors and that logic is already "on the shelf." Most C# books and articles I have seen use the default...
4
3099
by: Robert Schuldenfrei | last post by:
Dear NG, I was about to "improve" concurrency checking with a Timestamp when I discovered that my current code is not working. After about a day of beating my head against the wall, I am turning to the NG in hopes that someone can spot what I am doing wrong. Key to this technique working is the SQL UPDATE statement. It is designed to fail
8
5041
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my select, remember it, and then use it in the update. It works just fine when I have full control of the whole process. I want to do the same for my GridView/SqlDataSource combinations. I typically select from a view and update the corresponding...
4
2266
by: Jerry | last post by:
Hi, I have an app which retrieves data from a sql server table and displays it on a datagrid. If 2 sessions of this app are running and 2 users try to update the same record at about the same time, one of the apps will yield a concurrency violation error. The app with the error will have a little red error symbol next to the record in the datagrid, and the only way I can make the error go away for now is to restart that session. Is...
3
1430
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 concurrency error on mydataadapter.update() method. I know that there is no data concurrency problem as I am the only user testing the app. Obviously the error is misleading. What can I do from here to fix this problem? Thanks
5
1850
by: John | last post by:
Hi I have developed the following logic to handle db concurrency violations. I just wonder if someone can tell me if it is correct or if I need a different approach.Would love to know how pros handle it. Thanks Regards
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.