Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 310370 - Last Review: June 29, 2004 - Revision: 3.1
How To Retrieve Values from DataRows That Are Marked "Deleted" by Using Visual Basic .NET
This article was previously published under Q310370
On This Page
SUMMARY
You can use ADO.NET
DataTable objects to delete rows and mark them as "Deleted." This article
demonstrates how to retrieve the values of a row whose
DataRowState enumeration is set to
DataRowState.Deleted.
Description of the Technique
You can use the
DataRowVersion.Original enumeration value to retrieve the values of a row that has the
DataRowState.Deleted enumeration value. You must use
DataRowVersion.Original because the values of a deleted row are not available for the
current state (
DataRowVersion.Current).
If you try to reference current column values of a
DataRow object that has a
RowState property value of
DataRowState.Deleted, you receive the following error message:
An unhandled exception of type
'System.Data.DeletedRowInaccessibleException'
occurred in
system.data.dll
Additional information: Deleted row information can't be
accessed through the row.
Steps to Build the Sample
The sample code in this section demonstrates how to reproduce the
preceding error. This sample also demonstrates how to use
DataRowVersion.Original to obtain the column values of the deleted row.
- Create a new Visual Basic Console Application
project.
- Delete the default code, and add the following code to the Module1 code module:
Imports System.Data.SqlClient
Imports Console = System.Console
Module Module1
Sub Main()
Dim response As String
Dim myDataSet As New DataSet()
Dim myDataRow As DataRow
Dim newDataSet As DataSet
Dim rowsInError As DataRow()
Dim newTable As DataTable
Dim newCol As DataColumn
Dim i As Integer
Dim myCn As New SqlConnection()
myCn.ConnectionString = "Server=mySQLServer;User ID=sa;Password=mypassword;" & _
"Initial Catalog=Northwind;"
Dim myDACust As New SqlDataAdapter("Select * From Customers", myCn)
Dim myCmdBlder As New SqlCommandBuilder(myDACust)
myCn.Open()
myDACust.MissingSchemaAction = MissingSchemaAction.AddWithKey
myDACust.Fill(myDataSet, "Customers")
myDataSet.Tables("Customers").Rows(0).Delete()
Dim delRow As DataRow
If myDataSet.HasChanges(DataRowState.Deleted) Then
newDataSet = myDataSet.GetChanges(DataRowState.Deleted)
If (Not newDataSet.HasErrors) Then
delRow = newDataSet.Tables("Customers").Rows(0)
' The following line generates the exception:
Console.WriteLine(delRow(0))
' To resolve this problem, comment the previous line,
' and uncomment the following line.
' Console.WriteLine(delRow(0, DataRowVersion.Original))
End If
End If
If myCn.State = ConnectionState.Open Then
myCn.Close()
End If
myCn = Nothing
Console.WriteLine("Update was processed successfully!")
Console.ReadLine()
End Sub
End Module
- Modify the parameters of the ConnectionString property of the SqlConnection object as appropriate for your environment.
- In the Microsoft Visual Studio .NET Integrated Development
Environment (IDE), on the Debug menu, click Run Without Debugging to run the code. A console window displays the "Update was
processed successfully!" message if Visual Studio does not encounter any
errors.
- Press any key to dismiss the Console window and stop the
application.
REFERENCES
For
additional information, click the following article number to view the article
in the Microsoft Knowledge Base:
172375Â
(http://kbalertz.com/Feedback.aspx?kbNumber=172375/EN-US/
)
How To Undo Updates for a Single Row with CancelBatch
For more information, see the "Row States and Row
Versions" topic and other topics in the Microsoft .NET Framework
documentation.
APPLIES TO
- Microsoft ADO.NET 1.1
- Microsoft ADO.NET 1.0
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
| kbhowtomaster kbsqlclient kbsystemdata kbconsole KB310370 |
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