Solved

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

Posted on 2004-03-29
8
423 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

617 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