Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 310375 - Last Review: May 13, 2007 - Revision: 2.2
Error message when you update a database by using a stored procedure: "System.Data.DBConcurrencyException"
This article was previously published under Q310375
This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- System.Data.OleDb
- System.Data.SqlClient
On This Page
SYMPTOMS
When you use a
DataAdapter object to write changes back to a database by means of a stored
procedure, you may receive the following error message:
An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in
system.data.dll
Additional information: Concurrency violation: the
UpdateCommand affected 0 records.
If the data is not updated
correctly, you should receive a DataConcurrencyException exception instead of
the above-mentioned error message.
CAUSE
This problem can occur if the stored procedure runs the SET
NOCOUNT ON statement.
RESOLUTION
To receive an exception when the data is not updated
properly, you must explicitly add the SET NOCOUNT OFF statement to the stored
procedure.
STATUS
This
behavior is by design.
MORE INFORMATION
To determine if a concurrency problem exists, the
DataAdapter relies on the
RecordsAffected property that the stored procedure returns. If you set NOCOUNT to
ON, the stored procedure does not return the correct number of affected
records, and the
DataAdapter cannot determine that an error has occurred.
Steps to Reproduce the Behavior
This sample code uses the Employees table of the Northwind sample
database that is included with Microsoft SQL Server.
- Paste the following statements into the SQL Query Analyzer
tool or the ISQL utility:
CREATE PROCEDURE UpdateEmployee
AS
SET NOCOUNT ON
UPDATE Employees SET FirstName = 'Jay' WHERE EmployeeID = 1000
SELECT * FROM employees
GO
- Open Microsoft Visual Studio .NET.
- Create a new Visual Basic Windows Application
project.
- Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does
not.
- Place a Button control on Form1. Change the Name property of the button to btnTest, and
change the Text property to Test.
- Use the Imports statement on the System, System.Data, and System.Data.SqlClient namespaces so that you are not required to qualify declarations
in those namespaces later in your code. Add the following code to the "General
Declarations" section of Form1:
Imports System
Imports System.Data.OleDb
Imports System.Data.SqlClient
- Add the following code after the "Windows Form Designer
generated code" region in the Code window:
Note You must change the User ID <user
name> and Password <strong password> to the correct values. Make sure
that the User IdD has the appropriate permissions to perform these operations
on the database.
Private Sub btnTest_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnTest.Click
Dim myConnString As String = _
"User ID=<username>;Password=<strong password>;Initial Catalog=Northwind;Data Source=myServer"
Dim myQuery As String = _
"SELECT * FROM Employees WHERE employeeid = 1"
Dim cn As New SqlConnection(myConnString)
cn.Open()
Dim da As New SqlDataAdapter()
da.SelectCommand = New SqlCommand(myQuery, cn)
da.UpdateCommand = New SqlCommand()
With da.UpdateCommand
.Connection = cn
.CommandType = CommandType.StoredProcedure
.CommandText = "UpdateEmployee"
End With
Dim ds As New DataSet()
da.Fill(ds, "EmployeeTable")
ds.Tables("EmployeeTable").Rows(0)("FirstName") = "Jack"
Dim iRowsAffected As Integer
iRowsAffected = da.Update(ds, "EmployeeTable")
MessageBox.Show(iRowsAffected.ToString)
cn.Close()
End Sub
- Modify the connection string (myConnString) as appropriate for your environment.
- Save your project. On the Debug menu, click Start to run your project.
- Click Test. Notice that the exception is not generated even if the stored
procedure tries to update a record that is not present.
- Add the SET NOCOUNT OFF statement to the stored procedure,
and then click Test again. Notice that the DataConcurrencyException exception is
generated as expected.
REFERENCES
For more information about ADO.NET objects and syntax,
refer to the following Microsoft .NET Framework Software Development Kit (SDK)
documentation:
For
additional information about how to update a SQL Server database, click the
article number below to view the article in the Microsoft Knowledge Base:
308055Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308055/EN-US/
)
HOW TO: Update a SQL Server Database by Using the SqlDataAdapter Object in Visual Basic .NET
APPLIES TO
- Microsoft ADO.NET (included with the .NET Framework)
- Microsoft Visual Basic .NET 2002 Standard Edition
| kbsqlclient kbsystemdata kbprb KB310375 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please