Solved

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

Posted on 2004-03-29
8
415 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
Comment Utility
I did not try it, but give it a shot

ds.RejectChanges
0
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 41 total points
Comment Utility
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
Comment Utility
Hi huer,
You could save the DataSet to Xml, then if the update fails clear the dataset and reread it from XML

Dabas
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Expert Comment

by:EBatista
Comment Utility
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
Comment Utility
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
Comment Utility
save
ds.WriteXML("YourXMLfile")

to read back to ds

ds.ReadXML("YourXMLfile")
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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