Solved

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

Posted on 2004-03-29
8
420 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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 …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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