Quick Objects Documentation Akal Tech Logo
Inserting a Single New Record

Glossary Item Box

Business Logic Framework - Tutorial 9:

Our ninth tutorial shows you how to save a single record to the database and get the primary key value of the newly inserted record. The user enters values the First Name and Last name, clicks on the Save button. The code below shows how the sample will insert a new customer record and then display the Customer ID value of this customer.

Here is the example code that inserts a new Customer record into the database:

1 using (Customers c = new Customers())  
2 {  
3     // By setting the ObjectMode property to ObjectModes.Save, you are indicating to the business  
4     // object that it is going to treat all values assigned to its properties as the ones it will use  
5     // in the save process (i.e. Insert in this example)  
6     c.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save;  
7       
8     // Assign the values to the properties  
9     // Since the business object's ObjectMode is set to Save, each of these field's UseInSave properties  
10     // will be automatically switched to true.  
11     c.FirstName.Value = this.TextBox_FirstName.Text;  
12     c.LastName.Value = this.TextBox_LastName.Text;  
13       
14     // Insert returns a bool value indicating if it was able to insert a new record or not.  
15     if (!c.Insert())  
16         // show the error message if the customer record did not get created.  
17         MessageBox.Show("Customer record did not get saved.\nReason: " + c.ErrorString);  
18     else 
19         // show the CustomerID value for the newly inserted record.  
20         this.Label_CustomerID.Text = c.CustomerID.Value.ToString();  
21 }  
22  
1 Using c As Customers = New Customers()  
2     ' By setting the ObjectMode property to ObjectModes.Save, you are indicating to the business  
3     ' object that it is going to treat all values assigned to its properties as the ones it will use  
4     ' in the save process (i.e. Insert in this example)  
5     c.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Save  
6  
7     ' Assign the values to the properties  
8     ' Since the business object's ObjectMode is set to Save, each of these field's UseInSave properties  
9     ' will be automatically switched to true.  
10     c.FirstName.Value = Me.TextBox_FirstName.Text  
11     c.LastName.Value = Me.TextBox_LastName.Text  
12  
13     ' Insert returns a bool value indicating if it was able to insert a new record or not.  
14     If Not c.Insert() Then 
15         ' show the error message if the customer record did not get created.  
16         MessageBox.Show("Customer record did not get saved.\nReason: " + c.ErrorString)  
17     Else 
18         ' show the CustomerID value for the newly inserted record.  
19         Me.Label_CustomerID.Text = c.CustomerID.Value.ToString()  
20     End If 
21 End Using  
22  

 

 This code above shows that just by writing a three lines of code we are able to assign an object's property value and then with one method call were able to save a new record and get the primary key value back in the same call.

 
As you can see in the above screen shot we are specifying values for the First Name and Last Name fields and the CustomerID label is currently blank. However, after the Save button is clicked the Insert command is run and then the CustomerID values is retrieved which is shown below.