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:
| if (this.TextBox_CustomerID.Text.Trim().Length > 0) | |
| { | |
| using (Customers customer = new Customers()) | |
| { | |
| // This line assigns the Primary Key value specified by the user | |
| // to the CustomerID property of the customer object. | |
| customer.CustomerID.Parse(this.TextBox_CustomerID.Text); | |
| // The following line calls the method on the customer object which will | |
| // join the two objects i.e. customer and CustomerID_Orders_Child (stored in customer's property) | |
| customer.Join_CustomerID_Orders_Child(); | |
| // Since the above line has already created a Join with the customer object, calling the Load method | |
| // on customer object will result in another DataTable in the ResultSet. This DataTable will be named | |
| // Orders and it will contain all the order records that belong to the specified CustomerID. | |
| if (customer.Load()) | |
| { | |
| this.TextBox_FirstName.Text = customer.FirstName.Value; | |
| this.TextBox_LastName.Text = customer.LastName.Value; | |
| // The following line ensures that the ResultSet actually contains a DataTable that represents | |
| // the CustomerID_Orders_Child (of type Orders). | |
| if (customer.ResultSet.Tables.Contains(customer.CustomerID_Orders_Child.GetResultSetName())) | |
| { | |
| this.GridView.DataSource = customer.ResultSet; | |
| // Since the ResultSet may contain more than 1 DataTable we explicitly specify | |
| // which DataTable should be used by the GridView. | |
| this.GridView.DataMember = customer.CustomerID_Orders_Child.GetResultSetName(); | |
| } | |
| } | |
| } | |
| } | |
| If Me.TextBox_CustomerID.Text.Trim().Length > 0 Then | |
| Using customer As Customers = New Customers() | |
| ' This line assigns the Primary Key value specified by the user | |
| ' to the CustomerID property of the customer object. | |
| customer.CustomerID.Parse(Me.TextBox_CustomerID.Text) | |
| ' The following line calls the method on the customer object which will | |
| ' join the two objects i.e. customer and CustomerID_Orders_Child (stored in customer's property) | |
| customer.Join_CustomerID_Orders_Child() | |
| ' Since the above line has already created a Join with the customer object, calling the Load method | |
| ' on customer object will result in another DataTable in the ResultSet. This DataTable will be named | |
| ' Orders and it will contain all the order records that belong to the specified CustomerID. | |
| If customer.Load() Then | |
| Me.TextBox_FirstName.Text = customer.FirstName.Value | |
| Me.TextBox_LastName.Text = customer.LastName.Value | |
| ' The following line ensures that the ResultSet actually contains a DataTable that represents | |
| ' the CustomerID_Orders_Child (of type Orders). | |
| If customer.ResultSet.Tables.Contains(customer.CustomerID_Orders_Child.GetResultSetName()) Then | |
| Me.GridView.DataSource = customer.ResultSet | |
| ' Since the ResultSet may contain more than 1 DataTable we explicitly specify | |
| ' which DataTable should be used by the GridView. | |
| Me.GridView.DataMember = customer.CustomerID_Orders_Child.GetResultSetName() | |
| End If | |
| End If | |
| End Using | |
| 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. |

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.