Quick Objects Documentation Akal Tech Logo
Load a Parent Record and All Child Records

Glossary Item Box

Business Logic Framework - Tutorial 5:

Our fifth tutorial shows you one of the very essential features that loads an object data from the database and it can also load any child records for the specified parent record. The user enters a value for CustomerID (which is the primary key for the Customers table) and the Load method is called which loads all the values from the Customer record into each property and also adds a DataTable in the ResultSet property that contains all the order records for the specified CustomerID.

Here is the example code that loads the Customer object's values and all the Orders placed by the customer from the database:

1 if (this.TextBox_CustomerID.Text.Trim().Length > 0)  
2 {  
3     using (Customers customer = new Customers())  
4     {  
5         // This line assigns the Primary Key value specified by the user  
6         // to the CustomerID property of the customer object.  
7         customer.CustomerID.Parse(this.TextBox_CustomerID.Text);  
8           
9         // The following line calls the method on the customer object which will  
10         // join the two objects i.e. customer and CustomerID_Orders_Child (stored in customer's property)  
11         customer.Join_CustomerID_Orders_Child();  
12           
13         // Since the above line has already created a Join with the customer object, calling the Load method  
14         // on customer object will result in another DataTable in the ResultSet. This DataTable will be named  
15         // Orders and it will contain all the order records that belong to the specified CustomerID.  
16         if (customer.Load())  
17         {  
18             this.TextBox_FirstName.Text = customer.FirstName.Value;  
19             this.TextBox_LastName.Text = customer.LastName.Value;  
20  
21             // The following line ensures that the ResultSet actually contains a DataTable that represents  
22             // the CustomerID_Orders_Child (of type Orders).  
23             if (customer.ResultSet.Tables.Contains(customer.CustomerID_Orders_Child.GetResultSetName()))  
24             {  
25                 this.GridView.DataSource = customer.ResultSet;  
26                 // Since the ResultSet may contain more than 1 DataTable we explicitly specify  
27                 // which DataTable should be used by the GridView.  
28                 this.GridView.DataMember = customer.CustomerID_Orders_Child.GetResultSetName();  
29             }  
30         }  
31     }  
32 }  
33  
1 If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then 
2     Using customer As Customers = New Customers()  
3         ' This line assigns the Primary Key value specified by the user  
4         ' to the CustomerID property of the customer object.  
5         customer.CustomerID.Parse(Me.TextBox_CustomerID.Text)  
6  
7         ' The following line calls the method on the customer object which will  
8         ' join the two objects i.e. customer and CustomerID_Orders_Child (stored in customer's property)  
9         customer.Join_CustomerID_Orders_Child()  
10  
11         ' Since the above line has already created a Join with the customer object, calling the Load method  
12         ' on customer object will result in another DataTable in the ResultSet. This DataTable will be named  
13         ' Orders and it will contain all the order records that belong to the specified CustomerID.  
14         If customer.Load() Then 
15             Me.TextBox_FirstName.Text = customer.FirstName.Value  
16             Me.TextBox_LastName.Text = customer.LastName.Value  
17  
18             ' The following line ensures that the ResultSet actually contains a DataTable that represents  
19             ' the CustomerID_Orders_Child (of type Orders).  
20             If customer.ResultSet.Tables.Contains(customer.CustomerID_Orders_Child.GetResultSetName()) Then 
21                 Me.GridView.DataSource = customer.ResultSet  
22                 ' Since the ResultSet may contain more than 1 DataTable we explicitly specify  
23                 ' which DataTable should be used by the GridView.  
24                 Me.GridView.DataMember = customer.CustomerID_Orders_Child.GetResultSetName()  
25             End If 
26         End If 
27     End Using  
28 End If 

 

 This code above shows that just by writing a four lines of code we are able to get an object's all the values loaded into its properties and also get a list of child records from a child table. The Load method returns a Boolean value indicating weather the the values were loaded or not, but the Business Object also sets its IsLoaded property so you are able to refer to that property at a later stage as well. However, to ensure that the child records were returned or not, you must check the ResultSet DataSet and ensure that there is a DataTable that represents the child object.