Solved

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

Posted on 2004-03-29
8
416 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unable  to create new object 9 38
C# HTTP GET method sample code 3 37
SQL Exceptions 3 35
Automated testing suggestions? 2 20
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now