Quick Objects Documentation Akal Tech Logo
Delete a Single Record

Glossary Item Box

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.

1 private Customers customer = new Customers();  
2 private void Button_LoadCustomer_Click(object sender, EventArgs e)  
3 {  
4     if (this.TextBox_CustomerID.Text.Trim().Length > 0)  
5     {                  
6         // the following line displays a very useful method on each field type object  
7         // Parse method takes any string value and converts it to the correct type  
8         // and assigns the converted value into the Value property.  
9         customer.CustomerID.Parse(this.TextBox_CustomerID.Text);  
10  
11         // Load method expects that the primary key field(s) of the business object  
12         // have already been assigned values. Once the value is assigned   
13         // to the primary key fields, calling the Load method will load all field values  
14         // into the business object.  
15         if (customer.Load())  
16         {  
17             this.Button_Delete.Enabled = true;  
18             // Since we have already called the Load method and it was successful  
19             // we can now access the values of all the fields.  
20             this.TextBox_FirstName.Text = customer.FirstName.Value;  
21             this.TextBox_LastName.Text = customer.LastName.Value;  
22         }  
23         else 
24         {  
25             this.ResetTextBoxes();  
26             MessageBox.Show("Customer not found");  
27         }  
28     }  
29     else 
30     {  
31         this.ResetTextBoxes();  
32     }  
33 }  
34  
35 private void ResetTextBoxes()  
36 {  
37     this.TextBox_CustomerID.Enabled = true;  
38     //this.Button_Delete.Enabled = false;  
39     this.TextBox_FirstName.Text = "";  
40     this.TextBox_LastName.Text = "";  
41 }  
42  
43 private void Button_Delete_Click(object sender, EventArgs e)  
44 {  
45     if (customer.Delete())  
46     {  
47         MessageBox.Show("Customer Deleted!");  
48         this.ResetTextBoxes();  
49     }  
50     else 
51     {  
52         MessageBox.Show(customer.ErrorString);  
53     }  
54 }  
55  
1 Private customer As Customers =  New Customers()   
2 Private Sub Button_LoadCustomer_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button_LoadCustomer.Click  
3     If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then 
4         ' the following line displays a very useful method on each field type object  
5         ' Parse method takes any string value and converts it to the correct type  
6         ' and assigns the converted value into the Value property.  
7         customer.CustomerID.Parse(Me.TextBox_CustomerID.Text)  
8  
9         ' Load method expects that the primary key field(s) of the business object  
10         ' have already been assigned values. Once the value is assigned   
11         ' to the primary key fields, calling the Load method will load all field values  
12         ' into the business object.  
13         If customer.Load() Then 
14             Me.Button_Delete.Enabled = True 
15             ' Since we have already called the Load method and it was successful  
16             ' we can now access the values of all the fields.  
17             Me.TextBox_FirstName.Text = customer.FirstName.Value  
18             Me.TextBox_LastName.Text = customer.LastName.Value  
19         Else 
20             Me.ResetTextBoxes()  
21             MessageBox.Show("Customer not found")  
22         End If 
23     Else 
24         Me.ResetTextBoxes()  
25     End If 
26 End Sub 
27  
28 Private  Sub ResetTextBoxes()  
29     Me.TextBox_CustomerID.Enabled = True 
30     'this.Button_Delete.Enabled = false;  
31     Me.TextBox_FirstName.Text = "" 
32     Me.TextBox_LastName.Text = "" 
33 End Sub 
34  
35 Private Sub Button_Delete_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button_Delete.Click  
36     If customer.Delete() Then 
37         MessageBox.Show("Customer Deleted!")  
38         Me.ResetTextBoxes()  
39     Else 
40         MessageBox.Show(customer.ErrorString)  
41     End If 
42 End Sub 
43  

 

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.