I have the following error message and I can't see where in code it could of
came from or what could of caused it. It happened randomly for one of our
users and no one else ever, and I am unable to reproduce it at all.

System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand
affected 0 records.

Now this was caused during the update method of a data adapter that is tied
to a table alled Categories... On the form where the data entry takes place
we have a brand new record called coverage, This is stored in the dataset's
coverage table. At this point this is in thet table as a new record with a
uniquely generated ID Primary key that gets changed when the dataset is
updated to the database (that part works fine, SQL Server returns the new ID
and the local dataset updates approperiatly to match)... the problem though
seems to of occured on the referenced table Categories... This table has
records that are dynamically generated based on what a user checkes in a
check listbox. So if the user unchecks an item, the corresponding record is
removed from the datatable in teh categories table, if they check one a
record gets added... here is an example piece of code...

Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemCheckEventArgs) Handles chklstCategories.ItemCheck

' update dataset in memory to reflect changes

If Me.b_refillingData = False Then

Select Case e.NewValue

Case CheckState.Checked

Dim dr_catItem As DataRow =
ds_formData.Tables("CoverageJunCategories").NewRow

If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then

'dr_CoverageRow

dr_catItem("coverageid") = Convert.ToInt32(dr_CoverageRow("CoverageID"))

Else

dr_catItem("coverageid") =
Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)

End If

dr_catItem("categoryid") = DirectCast(Me.chklstCategories.Items(e.Index),
CategoryItem).ID

ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)

Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)

Case CheckState.Unchecked

Dim rowKeys(1) As Object

If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then

rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))

Else

rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)

End If

rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index), CategoryItem).ID



Dim dr_catitem As DataRow =
ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)

dr_catitem.Delete()

Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)

End Select

End If

End Sub



as you can see this is a junction table which joins the list of categories
and the list of coverages together.. In our dataset we also have the
coverages and this table referenced like this

ds_formData.Relations.Add("CoveragesJunCategories",
ds_formData.Tables("Coverages").Columns("CoverageID"),
ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))

to me this code all seems to be correct, but I can't figure out what is
wrong... prior to this we were getting a "Missing update command" error for
the data adapter that handles the coveragesjuncategories table's... I
figured since all I was doing was adding and removing rows I only would need
a Select, Insert, and Delete command... Since it seemed like no real
updateing was occuring (unless when coverages gets its new primary key from
the server when a coverage row is created, it changes the category
junction's table also since they are referenced together with a
primary/foreign key, but if this is a update how would the row be created to
start with? just a little confused on the procedure for a new row in one
table that references a new row in another when the primary key gets updates
on the parent, what happens to the row the foreign key is on is it still an
insert or is it now an update (i assumed insert because the row does not
exist yet in the database only locally in the data table since the update
method as not been called yet on the categorys junction table (child table)
but only on the coverages one (parent table))... anyone that can help me out
would be great! thanks!

Re: Concurrency violation where there shouldn't be one by Brian

Brian
Mon Jun 27 09:12:53 CDT 2005

I have the following error message and I can't see where in code it could of
came from or what could of caused it. It happened randomly for one of our
users and no one else ever, and I am unable to reproduce it at all.

System.Data.DBConcurrencyException: Concurrency violation: the UpdateCommand
affected 0 records.

Now this was caused during the update method of a data adapter that is tied
to a table alled Categories... On the form where the data entry takes place
we have a brand new record called coverage, This is stored in the dataset's
coverage table. At this point this is in thet table as a new record with a
uniquely generated ID Primary key that gets changed when the dataset is
updated to the database (that part works fine, SQL Server returns the new ID
and the local dataset updates approperiatly to match)... the problem though
seems to of occured on the referenced table Categories... This table has
records that are dynamically generated based on what a user checkes in a
check listbox. So if the user unchecks an item, the corresponding record is
removed from the datatable in teh categories table, if they check one a
record gets added... here is an example piece of code...

Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemCheckEventArgs) Handles chklstCategories.ItemCheck

' update dataset in memory to reflect changes

If Me.b_refillingData = False Then

Select Case e.NewValue

Case CheckState.Checked

Dim dr_catItem As DataRow =
ds_formData.Tables("CoverageJunCategories").NewRow

If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then

'dr_CoverageRow

dr_catItem("coverageid") = Convert.ToInt32(dr_CoverageRow("CoverageID"))

Else

dr_catItem("coverageid") =
Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)

End If

dr_catItem("categoryid") = DirectCast(Me.chklstCategories.Items(e.Index),
CategoryItem).ID

ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)

Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)

Case CheckState.Unchecked

Dim rowKeys(1) As Object

If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then

rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))

Else

rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)

End If

rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index), CategoryItem).ID



Dim dr_catitem As DataRow =
ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)

dr_catitem.Delete()

Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)

End Select

End If

End Sub



as you can see this is a junction table which joins the list of categories
and the list of coverages together.. In our dataset we also have the
coverages and this table referenced like this

ds_formData.Relations.Add("CoveragesJunCategories",
ds_formData.Tables("Coverages").Columns("CoverageID"),
ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))

to me this code all seems to be correct, but I can't figure out what is
wrong... prior to this we were getting a "Missing update command" error for
the data adapter that handles the coveragesjuncategories table's... I
figured since all I was doing was adding and removing rows I only would need
a Select, Insert, and Delete command... Since it seemed like no real
updateing was occuring (unless when coverages gets its new primary key from
the server when a coverage row is created, it changes the category
junction's table also since they are referenced together with a
primary/foreign key, but if this is a update how would the row be created to
start with? just a little confused on the procedure for a new row in one
table that references a new row in another when the primary key gets updates
on the parent, what happens to the row the foreign key is on is it still an
insert or is it now an update (i assumed insert because the row does not
exist yet in the database only locally in the data table since the update
method as not been called yet on the categorys junction table (child table)
but only on the coverages one (parent table))... anyone that can help me out
would be great! thanks!



Re: Concurrency violation where there shouldn't be one by William

William
Mon Jun 27 11:43:22 CDT 2005

Question: Is the INSERT command being executed by a stored procedure? Is
this SP making changes to other tables or executing other action commands?
If so, this changes the last RowsAffected value returned to the Update
method and while the change has taken place, ADO.NET thinks it has not and
returns the exception you describe.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Brian Henry" <nospam@nospam.com> wrote in message
news:eJnpQJyeFHA.3028@TK2MSFTNGP09.phx.gbl...
>I have the following error message and I can't see where in code it could
>of
> came from or what could of caused it. It happened randomly for one of our
> users and no one else ever, and I am unable to reproduce it at all.
>
> System.Data.DBConcurrencyException: Concurrency violation: the
> UpdateCommand
> affected 0 records.
>
> Now this was caused during the update method of a data adapter that is
> tied
> to a table alled Categories... On the form where the data entry takes
> place
> we have a brand new record called coverage, This is stored in the
> dataset's
> coverage table. At this point this is in thet table as a new record with
> a
> uniquely generated ID Primary key that gets changed when the dataset is
> updated to the database (that part works fine, SQL Server returns the new
> ID
> and the local dataset updates approperiatly to match)... the problem
> though
> seems to of occured on the referenced table Categories... This table has
> records that are dynamically generated based on what a user checkes in a
> check listbox. So if the user unchecks an item, the corresponding record
> is
> removed from the datatable in teh categories table, if they check one a
> record gets added... here is an example piece of code...
>
> Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e As
> System.Windows.Forms.ItemCheckEventArgs) Handles
> chklstCategories.ItemCheck
>
> ' update dataset in memory to reflect changes
>
> If Me.b_refillingData = False Then
>
> Select Case e.NewValue
>
> Case CheckState.Checked
>
> Dim dr_catItem As DataRow =
> ds_formData.Tables("CoverageJunCategories").NewRow
>
> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>
> 'dr_CoverageRow
>
> dr_catItem("coverageid") = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>
> Else
>
> dr_catItem("coverageid") =
> Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>
> End If
>
> dr_catItem("categoryid") = DirectCast(Me.chklstCategories.Items(e.Index),
> CategoryItem).ID
>
> ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)
>
> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>
> Case CheckState.Unchecked
>
> Dim rowKeys(1) As Object
>
> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>
> rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>
> Else
>
> rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>
> End If
>
> rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index),
> CategoryItem).ID
>
>
>
> Dim dr_catitem As DataRow =
> ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)
>
> dr_catitem.Delete()
>
> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>
> End Select
>
> End If
>
> End Sub
>
>
>
> as you can see this is a junction table which joins the list of categories
> and the list of coverages together.. In our dataset we also have the
> coverages and this table referenced like this
>
> ds_formData.Relations.Add("CoveragesJunCategories",
> ds_formData.Tables("Coverages").Columns("CoverageID"),
> ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))
>
> to me this code all seems to be correct, but I can't figure out what is
> wrong... prior to this we were getting a "Missing update command" error
> for
> the data adapter that handles the coveragesjuncategories table's... I
> figured since all I was doing was adding and removing rows I only would
> need
> a Select, Insert, and Delete command... Since it seemed like no real
> updateing was occuring (unless when coverages gets its new primary key
> from
> the server when a coverage row is created, it changes the category
> junction's table also since they are referenced together with a
> primary/foreign key, but if this is a update how would the row be created
> to
> start with? just a little confused on the procedure for a new row in one
> table that references a new row in another when the primary key gets
> updates
> on the parent, what happens to the row the foreign key is on is it still
> an
> insert or is it now an update (i assumed insert because the row does not
> exist yet in the database only locally in the data table since the update
> method as not been called yet on the categorys junction table (child
> table)
> but only on the coverages one (parent table))... anyone that can help me
> out
> would be great! thanks!
>



Re: Concurrency violation where there shouldn't be one by Brian

Brian
Mon Jun 27 13:10:02 CDT 2005

yes, they are going through a stored proc.. below are my stored procedures
for these commands




= [ for category insertion ]================================
CREATE PROCEDURE [dbo].[BENESP_AddCategoryForCoverage]
@coverageid int,
@categoryid int
AS
insert into coveragejuncategories (coverageid, categoryid) values
(@coverageid, @categoryid)


=[ for coverage insertion (parent table]========================
CREATE PROCEDURE [DBO].[BENESP_AddAccountsCoverages]
@coverageid int output,
@accountid int,
@type int,
@active bit,
@consortiumid int,
@lastrenewaldate datetime,
@AquiredDate datetime,
@createddate datetime,
@aquiredvia varchar(100),
@formerbrokeragent int,
@inactivestate varchar(100),
@inactiveasof datetime,
@transferedto int,
@transferreason varchar(100),
@unionstatus varchar(100),
@paytype varchar(100),
@EmploymentStatus varchar(100),
@fundingmethod varchar(100),
@nextrenewaldate datetime,
@CommStartDate datetime,
@CommEndDate datetime,
@BillingCompanyID int,
@ProductID int,
@LastEditedBy varchar(50)
AS

declare @newcoverageid int


-- if this an active covearge make sure the accounts active state is true
if @active = 1
update accounts set inactive = 0 where accountid = @accountid

insert into coverages
(type,active,consortiumid,lastrenewaldate,AquiredDate,createddate,aquiredvia,formerbrokeragent,
inactivestate,inactiveasof,transferedto,transferreason,unionstatus,paytype,EmploymentStatus,fundingmethod,nextrenewaldate,
CommStartDate,CommEndDate,BillingCompanyID,ProductID,
LastEditedBy,LastEditedTime)

values
(@type,@active,@consortiumid,@lastrenewaldate,@AquiredDate,@createddate,@aquiredvia,@formerbrokeragent,
@inactivestate,@inactiveasof,@transferedto,@transferreason,@unionstatus,@paytype,@EmploymentStatus,@fundingmethod,@nextrenewaldate,
@CommStartDate,@CommEndDate,@BillingCompanyID,@ProductID,@LastEditedBy,getdate())
set @newcoverageid = scope_identity()


-- insert junction
insert into accountsjuncoverages (accountid, coverageid) values
(@accountid,@newcoverageid)


select @coverageid = @newcoverageid
GO


now that add coverage SP does make a change to another tabel called accounts
if the coverage for an account is inactive... could this have an effect?
thanks


"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:u4H4hdzeFHA.1136@TK2MSFTNGP12.phx.gbl...
> Question: Is the INSERT command being executed by a stored procedure? Is
> this SP making changes to other tables or executing other action commands?
> If so, this changes the last RowsAffected value returned to the Update
> method and while the change has taken place, ADO.NET thinks it has not and
> returns the exception you describe.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
>
> "Brian Henry" <nospam@nospam.com> wrote in message
> news:eJnpQJyeFHA.3028@TK2MSFTNGP09.phx.gbl...
>>I have the following error message and I can't see where in code it could
>>of
>> came from or what could of caused it. It happened randomly for one of our
>> users and no one else ever, and I am unable to reproduce it at all.
>>
>> System.Data.DBConcurrencyException: Concurrency violation: the
>> UpdateCommand
>> affected 0 records.
>>
>> Now this was caused during the update method of a data adapter that is
>> tied
>> to a table alled Categories... On the form where the data entry takes
>> place
>> we have a brand new record called coverage, This is stored in the
>> dataset's
>> coverage table. At this point this is in thet table as a new record with
>> a
>> uniquely generated ID Primary key that gets changed when the dataset is
>> updated to the database (that part works fine, SQL Server returns the new
>> ID
>> and the local dataset updates approperiatly to match)... the problem
>> though
>> seems to of occured on the referenced table Categories... This table has
>> records that are dynamically generated based on what a user checkes in a
>> check listbox. So if the user unchecks an item, the corresponding record
>> is
>> removed from the datatable in teh categories table, if they check one a
>> record gets added... here is an example piece of code...
>>
>> Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e As
>> System.Windows.Forms.ItemCheckEventArgs) Handles
>> chklstCategories.ItemCheck
>>
>> ' update dataset in memory to reflect changes
>>
>> If Me.b_refillingData = False Then
>>
>> Select Case e.NewValue
>>
>> Case CheckState.Checked
>>
>> Dim dr_catItem As DataRow =
>> ds_formData.Tables("CoverageJunCategories").NewRow
>>
>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>
>> 'dr_CoverageRow
>>
>> dr_catItem("coverageid") = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>
>> Else
>>
>> dr_catItem("coverageid") =
>> Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>
>> End If
>>
>> dr_catItem("categoryid") = DirectCast(Me.chklstCategories.Items(e.Index),
>> CategoryItem).ID
>>
>> ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)
>>
>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>
>> Case CheckState.Unchecked
>>
>> Dim rowKeys(1) As Object
>>
>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>
>> rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>
>> Else
>>
>> rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>
>> End If
>>
>> rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index),
>> CategoryItem).ID
>>
>>
>>
>> Dim dr_catitem As DataRow =
>> ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)
>>
>> dr_catitem.Delete()
>>
>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>
>> End Select
>>
>> End If
>>
>> End Sub
>>
>>
>>
>> as you can see this is a junction table which joins the list of
>> categories
>> and the list of coverages together.. In our dataset we also have the
>> coverages and this table referenced like this
>>
>> ds_formData.Relations.Add("CoveragesJunCategories",
>> ds_formData.Tables("Coverages").Columns("CoverageID"),
>> ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))
>>
>> to me this code all seems to be correct, but I can't figure out what is
>> wrong... prior to this we were getting a "Missing update command" error
>> for
>> the data adapter that handles the coveragesjuncategories table's... I
>> figured since all I was doing was adding and removing rows I only would
>> need
>> a Select, Insert, and Delete command... Since it seemed like no real
>> updateing was occuring (unless when coverages gets its new primary key
>> from
>> the server when a coverage row is created, it changes the category
>> junction's table also since they are referenced together with a
>> primary/foreign key, but if this is a update how would the row be created
>> to
>> start with? just a little confused on the procedure for a new row in one
>> table that references a new row in another when the primary key gets
>> updates
>> on the parent, what happens to the row the foreign key is on is it still
>> an
>> insert or is it now an update (i assumed insert because the row does not
>> exist yet in the database only locally in the data table since the update
>> method as not been called yet on the categorys junction table (child
>> table)
>> but only on the coverages one (parent table))... anyone that can help me
>> out
>> would be great! thanks!
>>
>
>



Re: Concurrency violation where there shouldn't be one by William

William
Mon Jun 27 13:16:05 CDT 2005

Yup. That's it. ADO is looking at the wrong rowsaffected value. You might be
able to trick it by forcing it to 1 when returning from the SP or by using
your own custom exception/event handlers to deal with the issue.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Brian Henry" <nospam@nospam.com> wrote in message
news:ey%23dxN0eFHA.2732@TK2MSFTNGP14.phx.gbl...
> yes, they are going through a stored proc.. below are my stored procedures
> for these commands
>
>
>
>
> = [ for category insertion ]================================
> CREATE PROCEDURE [dbo].[BENESP_AddCategoryForCoverage]
> @coverageid int,
> @categoryid int
> AS
> insert into coveragejuncategories (coverageid, categoryid) values
> (@coverageid, @categoryid)
>
>
> =[ for coverage insertion (parent table]========================
> CREATE PROCEDURE [DBO].[BENESP_AddAccountsCoverages]
> @coverageid int output,
> @accountid int,
> @type int,
> @active bit,
> @consortiumid int,
> @lastrenewaldate datetime,
> @AquiredDate datetime,
> @createddate datetime,
> @aquiredvia varchar(100),
> @formerbrokeragent int,
> @inactivestate varchar(100),
> @inactiveasof datetime,
> @transferedto int,
> @transferreason varchar(100),
> @unionstatus varchar(100),
> @paytype varchar(100),
> @EmploymentStatus varchar(100),
> @fundingmethod varchar(100),
> @nextrenewaldate datetime,
> @CommStartDate datetime,
> @CommEndDate datetime,
> @BillingCompanyID int,
> @ProductID int,
> @LastEditedBy varchar(50)
> AS
>
> declare @newcoverageid int
>
>
> -- if this an active covearge make sure the accounts active state is true
> if @active = 1
> update accounts set inactive = 0 where accountid = @accountid
>
> insert into coverages
> (type,active,consortiumid,lastrenewaldate,AquiredDate,createddate,aquiredvia,formerbrokeragent,
>
> inactivestate,inactiveasof,transferedto,transferreason,unionstatus,paytype,EmploymentStatus,fundingmethod,nextrenewaldate,
> CommStartDate,CommEndDate,BillingCompanyID,ProductID,
> LastEditedBy,LastEditedTime)
>
> values
> (@type,@active,@consortiumid,@lastrenewaldate,@AquiredDate,@createddate,@aquiredvia,@formerbrokeragent,
>
> @inactivestate,@inactiveasof,@transferedto,@transferreason,@unionstatus,@paytype,@EmploymentStatus,@fundingmethod,@nextrenewaldate,
>
> @CommStartDate,@CommEndDate,@BillingCompanyID,@ProductID,@LastEditedBy,getdate())
> set @newcoverageid = scope_identity()
>
>
> -- insert junction
> insert into accountsjuncoverages (accountid, coverageid) values
> (@accountid,@newcoverageid)
>
>
> select @coverageid = @newcoverageid
> GO
>
>
> now that add coverage SP does make a change to another tabel called
> accounts if the coverage for an account is inactive... could this have an
> effect? thanks
>
>
> "William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
> news:u4H4hdzeFHA.1136@TK2MSFTNGP12.phx.gbl...
>> Question: Is the INSERT command being executed by a stored procedure? Is
>> this SP making changes to other tables or executing other action
>> commands? If so, this changes the last RowsAffected value returned to the
>> Update method and while the change has taken place, ADO.NET thinks it has
>> not and returns the exception you describe.
>>
>> --
>> ____________________________________
>> William (Bill) Vaughn
>> Author, Mentor, Consultant
>> Microsoft MVP
>> www.betav.com/blog/billva
>> www.betav.com
>> Please reply only to the newsgroup so that others can benefit.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>> __________________________________
>>
>> "Brian Henry" <nospam@nospam.com> wrote in message
>> news:eJnpQJyeFHA.3028@TK2MSFTNGP09.phx.gbl...
>>>I have the following error message and I can't see where in code it could
>>>of
>>> came from or what could of caused it. It happened randomly for one of
>>> our
>>> users and no one else ever, and I am unable to reproduce it at all.
>>>
>>> System.Data.DBConcurrencyException: Concurrency violation: the
>>> UpdateCommand
>>> affected 0 records.
>>>
>>> Now this was caused during the update method of a data adapter that is
>>> tied
>>> to a table alled Categories... On the form where the data entry takes
>>> place
>>> we have a brand new record called coverage, This is stored in the
>>> dataset's
>>> coverage table. At this point this is in thet table as a new record
>>> with a
>>> uniquely generated ID Primary key that gets changed when the dataset is
>>> updated to the database (that part works fine, SQL Server returns the
>>> new ID
>>> and the local dataset updates approperiatly to match)... the problem
>>> though
>>> seems to of occured on the referenced table Categories... This table has
>>> records that are dynamically generated based on what a user checkes in a
>>> check listbox. So if the user unchecks an item, the corresponding record
>>> is
>>> removed from the datatable in teh categories table, if they check one a
>>> record gets added... here is an example piece of code...
>>>
>>> Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e
>>> As
>>> System.Windows.Forms.ItemCheckEventArgs) Handles
>>> chklstCategories.ItemCheck
>>>
>>> ' update dataset in memory to reflect changes
>>>
>>> If Me.b_refillingData = False Then
>>>
>>> Select Case e.NewValue
>>>
>>> Case CheckState.Checked
>>>
>>> Dim dr_catItem As DataRow =
>>> ds_formData.Tables("CoverageJunCategories").NewRow
>>>
>>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>>
>>> 'dr_CoverageRow
>>>
>>> dr_catItem("coverageid") = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>>
>>> Else
>>>
>>> dr_catItem("coverageid") =
>>> Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>>
>>> End If
>>>
>>> dr_catItem("categoryid") =
>>> DirectCast(Me.chklstCategories.Items(e.Index),
>>> CategoryItem).ID
>>>
>>> ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)
>>>
>>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>>
>>> Case CheckState.Unchecked
>>>
>>> Dim rowKeys(1) As Object
>>>
>>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>>
>>> rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>>
>>> Else
>>>
>>> rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>>
>>> End If
>>>
>>> rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index),
>>> CategoryItem).ID
>>>
>>>
>>>
>>> Dim dr_catitem As DataRow =
>>> ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)
>>>
>>> dr_catitem.Delete()
>>>
>>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>>
>>> End Select
>>>
>>> End If
>>>
>>> End Sub
>>>
>>>
>>>
>>> as you can see this is a junction table which joins the list of
>>> categories
>>> and the list of coverages together.. In our dataset we also have the
>>> coverages and this table referenced like this
>>>
>>> ds_formData.Relations.Add("CoveragesJunCategories",
>>> ds_formData.Tables("Coverages").Columns("CoverageID"),
>>> ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))
>>>
>>> to me this code all seems to be correct, but I can't figure out what is
>>> wrong... prior to this we were getting a "Missing update command" error
>>> for
>>> the data adapter that handles the coveragesjuncategories table's... I
>>> figured since all I was doing was adding and removing rows I only would
>>> need
>>> a Select, Insert, and Delete command... Since it seemed like no real
>>> updateing was occuring (unless when coverages gets its new primary key
>>> from
>>> the server when a coverage row is created, it changes the category
>>> junction's table also since they are referenced together with a
>>> primary/foreign key, but if this is a update how would the row be
>>> created to
>>> start with? just a little confused on the procedure for a new row in one
>>> table that references a new row in another when the primary key gets
>>> updates
>>> on the parent, what happens to the row the foreign key is on is it still
>>> an
>>> insert or is it now an update (i assumed insert because the row does not
>>> exist yet in the database only locally in the data table since the
>>> update
>>> method as not been called yet on the categorys junction table (child
>>> table)
>>> but only on the coverages one (parent table))... anyone that can help me
>>> out
>>> would be great! thanks!
>>>
>>
>>
>
>



Re: Concurrency violation where there shouldn't be one by Brian

Brian
Mon Jun 27 13:38:30 CDT 2005

thanks for the help! Would never of caught that with out you

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:ODLyYR0eFHA.3040@TK2MSFTNGP14.phx.gbl...
> Yup. That's it. ADO is looking at the wrong rowsaffected value. You might
> be able to trick it by forcing it to 1 when returning from the SP or by
> using your own custom exception/event handlers to deal with the issue.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
>
> "Brian Henry" <nospam@nospam.com> wrote in message
> news:ey%23dxN0eFHA.2732@TK2MSFTNGP14.phx.gbl...
>> yes, they are going through a stored proc.. below are my stored
>> procedures for these commands
>>
>>
>>
>>
>> = [ for category insertion ]================================
>> CREATE PROCEDURE [dbo].[BENESP_AddCategoryForCoverage]
>> @coverageid int,
>> @categoryid int
>> AS
>> insert into coveragejuncategories (coverageid, categoryid) values
>> (@coverageid, @categoryid)
>>
>>
>> =[ for coverage insertion (parent table]========================
>> CREATE PROCEDURE [DBO].[BENESP_AddAccountsCoverages]
>> @coverageid int output,
>> @accountid int,
>> @type int,
>> @active bit,
>> @consortiumid int,
>> @lastrenewaldate datetime,
>> @AquiredDate datetime,
>> @createddate datetime,
>> @aquiredvia varchar(100),
>> @formerbrokeragent int,
>> @inactivestate varchar(100),
>> @inactiveasof datetime,
>> @transferedto int,
>> @transferreason varchar(100),
>> @unionstatus varchar(100),
>> @paytype varchar(100),
>> @EmploymentStatus varchar(100),
>> @fundingmethod varchar(100),
>> @nextrenewaldate datetime,
>> @CommStartDate datetime,
>> @CommEndDate datetime,
>> @BillingCompanyID int,
>> @ProductID int,
>> @LastEditedBy varchar(50)
>> AS
>>
>> declare @newcoverageid int
>>
>>
>> -- if this an active covearge make sure the accounts active state is true
>> if @active = 1
>> update accounts set inactive = 0 where accountid = @accountid
>>
>> insert into coverages
>> (type,active,consortiumid,lastrenewaldate,AquiredDate,createddate,aquiredvia,formerbrokeragent,
>>
>> inactivestate,inactiveasof,transferedto,transferreason,unionstatus,paytype,EmploymentStatus,fundingmethod,nextrenewaldate,
>> CommStartDate,CommEndDate,BillingCompanyID,ProductID,
>> LastEditedBy,LastEditedTime)
>>
>> values
>> (@type,@active,@consortiumid,@lastrenewaldate,@AquiredDate,@createddate,@aquiredvia,@formerbrokeragent,
>>
>> @inactivestate,@inactiveasof,@transferedto,@transferreason,@unionstatus,@paytype,@EmploymentStatus,@fundingmethod,@nextrenewaldate,
>>
>> @CommStartDate,@CommEndDate,@BillingCompanyID,@ProductID,@LastEditedBy,getdate())
>> set @newcoverageid = scope_identity()
>>
>>
>> -- insert junction
>> insert into accountsjuncoverages (accountid, coverageid) values
>> (@accountid,@newcoverageid)
>>
>>
>> select @coverageid = @newcoverageid
>> GO
>>
>>
>> now that add coverage SP does make a change to another tabel called
>> accounts if the coverage for an account is inactive... could this have an
>> effect? thanks
>>
>>
>> "William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
>> news:u4H4hdzeFHA.1136@TK2MSFTNGP12.phx.gbl...
>>> Question: Is the INSERT command being executed by a stored procedure? Is
>>> this SP making changes to other tables or executing other action
>>> commands? If so, this changes the last RowsAffected value returned to
>>> the Update method and while the change has taken place, ADO.NET thinks
>>> it has not and returns the exception you describe.
>>>
>>> --
>>> ____________________________________
>>> William (Bill) Vaughn
>>> Author, Mentor, Consultant
>>> Microsoft MVP
>>> www.betav.com/blog/billva
>>> www.betav.com
>>> Please reply only to the newsgroup so that others can benefit.
>>> This posting is provided "AS IS" with no warranties, and confers no
>>> rights.
>>> __________________________________
>>>
>>> "Brian Henry" <nospam@nospam.com> wrote in message
>>> news:eJnpQJyeFHA.3028@TK2MSFTNGP09.phx.gbl...
>>>>I have the following error message and I can't see where in code it
>>>>could of
>>>> came from or what could of caused it. It happened randomly for one of
>>>> our
>>>> users and no one else ever, and I am unable to reproduce it at all.
>>>>
>>>> System.Data.DBConcurrencyException: Concurrency violation: the
>>>> UpdateCommand
>>>> affected 0 records.
>>>>
>>>> Now this was caused during the update method of a data adapter that is
>>>> tied
>>>> to a table alled Categories... On the form where the data entry takes
>>>> place
>>>> we have a brand new record called coverage, This is stored in the
>>>> dataset's
>>>> coverage table. At this point this is in thet table as a new record
>>>> with a
>>>> uniquely generated ID Primary key that gets changed when the dataset is
>>>> updated to the database (that part works fine, SQL Server returns the
>>>> new ID
>>>> and the local dataset updates approperiatly to match)... the problem
>>>> though
>>>> seems to of occured on the referenced table Categories... This table
>>>> has
>>>> records that are dynamically generated based on what a user checkes in
>>>> a
>>>> check listbox. So if the user unchecks an item, the corresponding
>>>> record is
>>>> removed from the datatable in teh categories table, if they check one a
>>>> record gets added... here is an example piece of code...
>>>>
>>>> Private Sub chklstCategories_ItemCheck(ByVal sender As Object, ByVal e
>>>> As
>>>> System.Windows.Forms.ItemCheckEventArgs) Handles
>>>> chklstCategories.ItemCheck
>>>>
>>>> ' update dataset in memory to reflect changes
>>>>
>>>> If Me.b_refillingData = False Then
>>>>
>>>> Select Case e.NewValue
>>>>
>>>> Case CheckState.Checked
>>>>
>>>> Dim dr_catItem As DataRow =
>>>> ds_formData.Tables("CoverageJunCategories").NewRow
>>>>
>>>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>>>
>>>> 'dr_CoverageRow
>>>>
>>>> dr_catItem("coverageid") =
>>>> Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>>>
>>>> Else
>>>>
>>>> dr_catItem("coverageid") =
>>>> Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>>>
>>>> End If
>>>>
>>>> dr_catItem("categoryid") =
>>>> DirectCast(Me.chklstCategories.Items(e.Index),
>>>> CategoryItem).ID
>>>>
>>>> ds_formData.Tables("CoverageJunCategories").Rows.Add(dr_catItem)
>>>>
>>>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>>>
>>>> Case CheckState.Unchecked
>>>>
>>>> Dim rowKeys(1) As Object
>>>>
>>>> If Me.fs_FormViewState = bdb.Business.FormStates.AddNew Then
>>>>
>>>> rowKeys(0) = Convert.ToInt32(dr_CoverageRow("CoverageID"))
>>>>
>>>> Else
>>>>
>>>> rowKeys(0) = Convert.ToInt32(Me.lstvCoverages.SelectedItems(0).Tag)
>>>>
>>>> End If
>>>>
>>>> rowKeys(1) = DirectCast(Me.chklstCategories.Items(e.Index),
>>>> CategoryItem).ID
>>>>
>>>>
>>>>
>>>> Dim dr_catitem As DataRow =
>>>> ds_formData.Tables("CoverageJunCategories").Rows.Find(rowKeys)
>>>>
>>>> dr_catitem.Delete()
>>>>
>>>> Debug.WriteLine(ds_formData.Tables("CoverageJunCategories").Rows.Count)
>>>>
>>>> End Select
>>>>
>>>> End If
>>>>
>>>> End Sub
>>>>
>>>>
>>>>
>>>> as you can see this is a junction table which joins the list of
>>>> categories
>>>> and the list of coverages together.. In our dataset we also have the
>>>> coverages and this table referenced like this
>>>>
>>>> ds_formData.Relations.Add("CoveragesJunCategories",
>>>> ds_formData.Tables("Coverages").Columns("CoverageID"),
>>>> ds_formData.Tables("CoverageJunCategories").Columns("CoverageID"))
>>>>
>>>> to me this code all seems to be correct, but I can't figure out what is
>>>> wrong... prior to this we were getting a "Missing update command" error
>>>> for
>>>> the data adapter that handles the coveragesjuncategories table's... I
>>>> figured since all I was doing was adding and removing rows I only would
>>>> need
>>>> a Select, Insert, and Delete command... Since it seemed like no real
>>>> updateing was occuring (unless when coverages gets its new primary key
>>>> from
>>>> the server when a coverage row is created, it changes the category
>>>> junction's table also since they are referenced together with a
>>>> primary/foreign key, but if this is a update how would the row be
>>>> created to
>>>> start with? just a little confused on the procedure for a new row in
>>>> one
>>>> table that references a new row in another when the primary key gets
>>>> updates
>>>> on the parent, what happens to the row the foreign key is on is it
>>>> still an
>>>> insert or is it now an update (i assumed insert because the row does
>>>> not
>>>> exist yet in the database only locally in the data table since the
>>>> update
>>>> method as not been called yet on the categorys junction table (child
>>>> table)
>>>> but only on the coverages one (parent table))... anyone that can help
>>>> me out
>>>> would be great! thanks!
>>>>
>>>
>>>
>>
>>
>
>



Re: Concurrency violation where there shouldn't be one by Brian

Brian
Mon Jun 27 14:22:27 CDT 2005

one last question while I'm thinking about it, you would you trick the rows
changed number when it returns form an sp so ado.net doesn't mess up? thanks

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:ODLyYR0eFHA.3040@TK2MSFTNGP14.phx.gbl...
> Yup. That's it. ADO is looking at the wrong rowsaffected value. You might
> be able to trick it by forcing it to 1 when returning from the SP or by
> using your own custom exception/event handlers to deal with the issue.
>
> --
> ____________________________________
> William (Bill) Vaughn
> Author, Mentor, Consultant
> Microsoft MVP
> www.betav.com/blog/billva
> www.betav.com
> Please reply only to the newsgroup so that others can benefit.
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
> __________________________________
>
> "Brian Henry" <nospam@nospam.com> wrote in message
> news:ey%23dxN0eFHA.2732@TK2MSFTNGP14.phx.gbl...
>> yes, they are going through a stored proc.. below are my stored
>> procedures for these commands
>>
>>
>>
>>
>> = [ for category insertion ]================================
>> CREATE PROCEDURE [dbo].[BENESP_AddCategoryForCoverage]
>> @coverageid int,
>> @categoryid int
>> AS
>> insert into coveragejuncategories (coverageid, categoryid) values
>> (@coverageid, @categoryid)
>>
>>
>> =[ for coverage insertion (parent table]========================
>> CREATE PROCEDURE [DBO].[BENESP_AddAccountsCoverages]
>> @coverageid int output,
>> @accountid int,
>> @type int,
>> @active bit,
>> @consortiumid int,
>> @lastrenewaldate datetime,
>> @AquiredDate datetime,
>> @createddate datetime,
>> @aquiredvia varchar(100),
>> @formerbrokeragent int,
>> @inactivestate varchar(100),
>> @inactiveasof datetime,
>> @transferedto int,
>> @transferreason varchar(100),
>> @unionstatus varchar(100),
>> @paytype varchar(100),
>> @EmploymentStatus varchar(100),
>> @fundingmethod varchar(100),
>> @nextrenewaldate datetime,
>> @CommStartDate datetime,
>> @CommEndDate datetime,
>> @BillingCompanyID int,
>> @ProductID int,
>> @LastEditedBy varchar(50)
>> AS
>>
>> declare @newcoverageid int
>>
>>
>> -- if this an active covearge make sure the accounts active state is true
>> if @active = 1
>> update accounts set inactive = 0 where accountid = @accountid
>>
>> insert into coverages
>> (type,active,consortiumid,lastrenewaldate,AquiredDate,createddate,aquiredvia,formerbrokeragent,
>>
>> inactivestate,inactiveasof,transferedto,transferreason,unionstatus,paytype,EmploymentStatus,fundingmethod,nextrenewaldate,
>> CommStartDate,CommEndDate,BillingCompanyID,ProductID,
>> LastEditedBy,LastEditedTime)
>>
>> values
>> (@type,@active,@consortiumid,@lastrenewaldate,@AquiredDate,@createddate,@aquiredvia,@formerbrokeragent,
>>
>> @inactivestate,@inactiveasof,@transferedto,@transferreason,@unionstatus,@paytype,@EmploymentStatus,@fundingmethod,@nextrenewaldate,
>>
>> @CommStartDate,@CommEndDate,@BillingCompanyID,@ProductID,@LastEditedBy,getdate())
>> set @newcoverageid = scope_identity()
>>
>>
>> -- insert junction
>> insert into accountsjuncoverages (accountid, coverageid) values
>> (@accountid,@newcoverageid)
>>
>>
>> select @coverageid = @newcoverageid
>> GO
>>
>>
>> now that add coverage SP does make a change to another tabel called
>> accounts if the coverage for an account is inactive... could this have an
>> effect? thanks
>>
>>
>> "William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
>> news:u4H4hdzeFHA.1136@TK2MSFTNGP12.phx.gbl...
>>> Question: Is the INSERT command being executed by a stored procedure? Is
>>> this SP making changes to other tables or executing other action
>>> commands? If so, this changes the last RowsAffected value returned to
>>> the Update method and while the change has taken place, ADO.NET thinks
>>> it has not and returns the exception you describe.
>>>
>>> --
>>> ____________________________________
>>> William (Bill) Vaughn
>>> Author, Mentor, Consultant
>>> Microsoft MVP
>>> www.betav.com/blog/billva
>>> www.betav.com
>>> Please reply only to the newsgroup so that others can benefit.
>>> This posting is provided "AS IS" with no warranties, and confers no
>>> rights.
>>> __________________________________
>>>
>>> "Brian Henry" <nospam@nospam.com> wrote in message
>>> news:eJnpQJyeFHA.3028@TK2MSFTNGP09.phx.gbl...
>>&g