Solved

How to rollback the deleted record to DataSet in VB.NEt

Posted on 2004-03-29
8
417 Views
Last Modified: 2012-08-14


I have a code to delete a record from DataTable.  First I delete the record using
cmSchool.RemoveAt(cmSchool.Position), and the record is deleted from the DataTable. Then  I delete the record from the underlying database using  m_daSchool.Update(m_dsSchool, "School")
                        m_dsSchool.AcceptChanges()  
Since the record is referenced by children table, so it cannot be deleted.
But the record already disappeared from the DataTable.
Could you please tell me how i can rollback this record to the DataTable?
Thanks

Huer


Imports System
Imports System.IO

Public Class frmUtilities
    Inherits System.Windows.Forms.Form
    Private cmSchool As CurrencyManager
    Private m_dwSchool As DataView
Private Sub frmUtilities_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           '//-- bound to controls
        m_dwSchool =m_dsSchool.Tables("school").DefaultView
        m_dwSchool.Sort = "SCH_NAME"
        txtSchool.DataBindings.Add("text", m_dwSchool, "sch_name")
        txtSchoolNo.DataBindings.Add("text", m_dwSchool, "sch_id")
        cmSchool = Me.BindingContext(m_dwSchool)
     
    End Sub
 Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim Result As DialogResult
        Result = MessageBox.Show(Me, "Do you want to delete the record", "Delete Record", MessageBoxButtons.YesNo, _
               MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)
        If Result = DialogResult.No Then Exit Sub
        Try
            If (cmSchool.Count > 0) Then
                cmSchool.RemoveAt(cmSchool.Position)
                 m_daSchool.Update(m_dsSchool, "School")
                m_dsSchool.AcceptChanges()          
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            ' mvarConn.Close()
        End Try
    End Sub

     
0
Comment
Question by:huer
8 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 43 total points
ID: 10709949
I did not try it, but give it a shot

ds.RejectChanges
0
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 41 total points
ID: 10709955
first you dont need to call AcceptChanges() after the Updatemethod from the dataadapter cos it do it for you, so it is extra work.
now to the problem, the dataset provide the method Reject or RejectChange (dont have VS in this PC), you could place it in the catch block:
     
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            m_dsSchool.RejectChanges() ' or m_dsSchool.Reject()
       
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 41 total points
ID: 10710276
Hi huer,
You could save the DataSet to Xml, then if the update fails clear the dataset and reread it from XML

Dabas
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 10

Expert Comment

by:EBatista
ID: 10710374
btw be carefull how you use RejectChanges() cos it will reject all the changes you made to the dataset from the las time was invoked AceptChanges (recall that AcceptChanges is implicitly called from the dataadapter update method), that means that you will lost all the updates and inserts too, you can address that problem doing what dabas says or reflect first all those updated and inserted rows in the database

regards
0
 

Author Comment

by:huer
ID: 10714119
Could you tell me how to save the DataSet to xml, and then reread it from XML when update fails?
I am new to VB.NET

Please help me.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 10715493
save
ds.WriteXML("YourXMLfile")

to read back to ds

ds.ReadXML("YourXMLfile")
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question