Business Logic Framework - Tutorial 10:
Our tenth tutorial shows you how to delete a single record from the database. The user enters values the Customer ID, clicks on the Delete button. The code below shows how the sample will delete the customer record for the specified Customer ID value.
| private Customers customer = new Customers(); | |
| private void Button_LoadCustomer_Click(object sender, EventArgs e) | |
| { | |
| if (this.TextBox_CustomerID.Text.Trim().Length > 0) | |
| { | |
| // the following line displays a very useful method on each field type object | |
| // Parse method takes any string value and converts it to the correct type | |
| // and assigns the converted value into the Value property. | |
| customer.CustomerID.Parse(this.TextBox_CustomerID.Text); | |
| // Load method expects that the primary key field(s) of the business object | |
| // have already been assigned values. Once the value is assigned | |
| // to the primary key fields, calling the Load method will load all field values | |
| // into the business object. | |
| if (customer.Load()) | |
| { | |
| this.Button_Delete.Enabled = true; | |
| // Since we have already called the Load method and it was successful | |
| // we can now access the values of all the fields. | |
| this.TextBox_FirstName.Text = customer.FirstName.Value; | |
| this.TextBox_LastName.Text = customer.LastName.Value; | |
| } | |
| else | |
| { | |
| this.ResetTextBoxes(); | |
| MessageBox.Show("Customer not found"); | |
| } | |
| } | |
| else | |
| { | |
| this.ResetTextBoxes(); | |
| } | |
| } | |
| private void ResetTextBoxes() | |
| { | |
| this.TextBox_CustomerID.Enabled = true; | |
| //this.Button_Delete.Enabled = false; | |
| this.TextBox_FirstName.Text = ""; | |
| this.TextBox_LastName.Text = ""; | |
| } | |
| private void Button_Delete_Click(object sender, EventArgs e) | |
| { | |
| if (customer.Delete()) | |
| { | |
| MessageBox.Show("Customer Deleted!"); | |
| this.ResetTextBoxes(); | |
| } | |
| else | |
| { | |
| MessageBox.Show(customer.ErrorString); | |
| } | |
| } | |
| Private customer As Customers = New Customers() | |
| Private Sub Button_LoadCustomer_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_LoadCustomer.Click | |
| If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then | |
| ' the following line displays a very useful method on each field type object | |
| ' Parse method takes any string value and converts it to the correct type | |
| ' and assigns the converted value into the Value property. | |
| customer.CustomerID.Parse(Me.TextBox_CustomerID.Text) | |
| ' Load method expects that the primary key field(s) of the business object | |
| ' have already been assigned values. Once the value is assigned | |
| ' to the primary key fields, calling the Load method will load all field values | |
| ' into the business object. | |
| If customer.Load() Then | |
| Me.Button_Delete.Enabled = True | |
| ' Since we have already called the Load method and it was successful | |
| ' we can now access the values of all the fields. | |
| Me.TextBox_FirstName.Text = customer.FirstName.Value | |
| Me.TextBox_LastName.Text = customer.LastName.Value | |
| Else | |
| Me.ResetTextBoxes() | |
| MessageBox.Show("Customer not found") | |
| End If | |
| Else | |
| Me.ResetTextBoxes() | |
| End If | |
| End Sub | |
| Private Sub ResetTextBoxes() | |
| Me.TextBox_CustomerID.Enabled = True | |
| 'this.Button_Delete.Enabled = false; | |
| Me.TextBox_FirstName.Text = "" | |
| Me.TextBox_LastName.Text = "" | |
| End Sub | |
| Private Sub Button_Delete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Delete.Click | |
| If customer.Delete() Then | |
| MessageBox.Show("Customer Deleted!") | |
| Me.ResetTextBoxes() | |
| Else | |
| MessageBox.Show(customer.ErrorString) | |
| End If | |
| End Sub | |

First we provided the primary key value i.e. CustomerID = 5 and then clicked on the Load Customer button. The Load Customer button gets the object values from the database and the First Name and Last Name values are displayed.

Then clicking on the Delete button deletes the customer record for CustomerID 5. This sample however does not perform any concurrency checking. To perform concurrency checking look at the Tutorial 11 for details on how to use concurrency checking in UPDATE or DELETE commands.

