Microsoft Knowledge Base Email Alertz

KBAlertz.com: (310375) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








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.
  1. 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
    					
  2. Open Microsoft Visual Studio .NET.
  3. Create a new Visual Basic Windows Application project.
  4. Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.
  5. Place a Button control on Form1. Change the Name property of the button to btnTest, and change the Text property to Test.
  6. 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
    					
  7. 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
    					
  8. Modify the connection string (myConnString) as appropriate for your environment.
  9. Save your project. On the Debug menu, click Start to run your project.
  10. Click Test. Notice that the exception is not generated even if the stored procedure tries to update a record that is not present.
  11. 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:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)
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
Keywords: 
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