Data Set not Refreshing after Delete

tpra21

Active member
Joined
Oct 21, 2006
Messages
26
Programming Experience
1-3
In the code below, I have two textboxes and a combo box all bound to columns in the Reminders table. My ADD and UPDATE buttons work perfect, and refresh the data in my bound controls afterward. I cannot for the life of me get my DELETE button to refresh my bound controls after I click the delete button and delete a row. The deleted row is still in my controls until I exit from the form and reenter. I have tried clearing the data set, re-filling, etc. I cannot get anything to work. Any ideas would be great.

VB.NET:
Expand Collapse Copy
[SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Delete the record from the database
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbDeleteCommand1.CommandText = "DELETE * FROM Reminders WHERE (ReminderID = ?)"
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbDeleteCommand1.Connection = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbConnection2
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbDeleteCommand1.Parameters.Add("@ReminderID", System.Data.OleDb.OleDbType.Integer, 0, "ReminderID").Value = ComboBox1.SelectedValue
[/SIZE][SIZE=2][COLOR=#008000]'Give a warning message and ask for confirmation
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] result [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DialogResult = _
MessageBox.Show("Delete the Reminder ?", "Confirm Deletion", _
MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
[/SIZE][SIZE=2][COLOR=#008000]'If yes is entered on message, delete the record
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] result = DialogResult.Yes [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]OleDbConnection2.Open()
OleDbDeleteCommand1.ExecuteNonQuery()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
MessageBox.Show(ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]Finally
[/COLOR][/SIZE][SIZE=2]OleDbConnection2.Close()
MessageBox.Show("The Record Has Been Deleted", "Reminder Deleted", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
[/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].OleDbDataAdapter1.Fill([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Reminders1)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
 
Chances are that the row isn't getting deleted in the first place. I take you have checked to make sure that they are? The problem you may have is that the code in your Finally block is going to tell you that the delete worked whether it did or not. Remember the code in a Finally block always executes, even if an exception is thrown.
 
The row is getting deleted because when I look in the data base, it is gone. Plus, if I exit the form and then return (without exiting the program) the data is updated to reflect the deleted row. I however did not realize that the finally block is always executed, so I will look into that.

Thanks, Adam
 
Back
Top