
Signature
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Hello!
Sorry for the delay. Had some other issues a few days, but now I'm back online.
I still think I should use the triggers, If I could solve this problem.
Yes, the problem still exists. I will below add a routine for reproducing the problem.
I would be very glad if you can help me, because this is very annoying for me.
1. Create a database called UpdateTriggerProblem (this was made using MS SQL Server 2000)
2. Add a table named MyTbl with the three columns CustID (varchar(50) Primary key), CustName(varchar(50)), ChangedDate(datetime)
3. Add a trigger as follows
CREATE trigger [tri_MyTbl_update] on dbo.MyTbl for update as
declare @ID varchar(50)
select @ID=(select [CustID] from inserted)
update [MyTbl] set ChangedDate=GetDate()
where [CustID]=@ID
2. Create a new VB application with a form.
2. Add a datagrid and a button to the form
3. Add the code below in the form code
Dim ds As New DataSet
Dim cn As New SqlClient.SqlConnection("integrated security=SSPI;data source=localhost;initial catalog=UpdateTriggerProblem")
Dim da As New SqlClient.SqlDataAdapter("select * from MyTbl", cn)
Dim cb As New SqlClient.SqlCommandBuilder(da)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
da.Fill(ds, "MyTbl")
DataGrid1.DataSource = ds.Tables("MyTbl")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ds.HasChanges Then
cb.RefreshSchema()
da.Update(ds, "MyTbl")
End If
End Sub
4. Change the data source at second row (localhost) if necessary.
Now it is ready for testing. To test the project do as follows (always change the rowfocus before pressing the button).
1. Run project
2. Add the first row by adding values in CustID and CustName, and press the button
3. Change the CustName, and press the button
4. Do the 3:rd item again, and again if necessary
You should have got the problem by now.
Best regards Magnus
> Hi MB,
>
[quoted text clipped - 5 lines]
> ======> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
Kevin Yu [MSFT] - 23 Mar 2004 17:58 GMT
Hi MB,
Thanks for your steps to reproduce the problem. It makes things easier for
me to debug, and I have found the cause of the DBConcurrencyException.
I reproduced the problem with 2 trials of updating. The first time we
update the data source, the new value is written. Then the trigger comes
up. It modifies the ChangedDate field of that record. When the second
update fires, the DataAdapter found that the record in data source has been
changed, so an DBConcurrencyException was thrown.
To avoid this, we have to refresh the data in application after each
update. Here is a code snippet which refreshes the single row by the
primary key.
Private Sub UpdateRow(ByVal TableName As String, ByVal ID As String)
'Get a reference to the specified row
Dim dr As DataRow = dsAllData.Tables(TableName).Rows.Find(ID)
'Create a Command update to pull the new underlying data
Dim cmd As New SqlClient.SqlCommand("SELECT * FROM " & TableName & _
" WHERE ID=" & ID, connCustSvc)
'Open the connection and create the DataReader
connCustSvc.Open()
Dim rdr As SqlClient.SqlDataReader = cmd.ExecuteReader()
rdr.Read()
'Copy the new data from the database to the DataRow
Dim dc As DataColumn
For Each dc In dr.Table.Columns
If dc.ReadOnly = False Then _
dr.Item(dc.ColumnName) = rdr.Item(dc.ColumnName)
Next
'Accept changes in the DataRow
dr.AcceptChanges()
connCustSvc.Close()
End Sub
For more information about tackling data concurrency exceptions using the
dataSet object, please check the following article. It is a good article
which explains the cause of difference concurrency issue for you.
http://msdn.microsoft.com/msdnmag/issues/03/04/dataconcurrency/default.aspx
HTH. If anything is unclear, please feel free to reply to the post.
Kevin Yu

Signature
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Kevin Yu [MSFT] - 25 Mar 2004 14:47 GMT
Hi MB,
I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.
Kevin Yu

Signature
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."