Microsoft KB Archive/310370

= How To Retrieve Values from DataRows That Are Marked &quot;Deleted&quot; by Using Visual Basic .NET =

Article ID: 310370

Article Last Modified on 6/29/2004

-

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

-



This article was previously published under Q310370



This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data.SqlClient
 * System.Console

IN THIS TASK
SUMMARY
 * Description of the Technique
 * Steps to Build the Sample

REFERENCES



SUMMARY
You can use ADO.NET DataTable objects to delete rows and mark them as &quot;Deleted.&quot; This article demonstrates how to retrieve the values of a row whose DataRowState enumeration is set to DataRowState.Deleted.

back to the top

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.

back to the top

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 = &quot;Server=mySQLServer;User ID=sa;Password=mypassword;&quot; & _ &quot;Initial Catalog=Northwind;&quot; Dim myDACust As New SqlDataAdapter(&quot;Select * From Customers&quot;, myCn) Dim myCmdBlder As New SqlCommandBuilder(myDACust)

myCn.Open myDACust.MissingSchemaAction = MissingSchemaAction.AddWithKey myDACust.Fill(myDataSet, &quot;Customers&quot;) myDataSet.Tables(&quot;Customers&quot;).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(&quot;Customers&quot;).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(&quot;Update was processed successfully!&quot;) 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 &quot;Update was processed successfully!&quot; message if Visual Studio does not encounter any errors. Press any key to dismiss the Console window and stop the application.

back to the top

