Business Logic Framework - Tutorial 6:
Our sixth tutorial shows you one of the very powerful features that search for data based on varying criteria. The user enters the value for First Name and/or Last Name and also selects weather he/she wants to search for both the values or either one. In addition the user can also decide weather to match the values exactly or weather the system should do partial matches as well.
Here is the example code that performs this search based on criteria and settings specified by the user:
| // ensure that user has specified at least one criteria. | |
| if (this.TextBox_FirstName.Text.Trim().Length > 0 || this.TextBox_LastName.Text.Trim().Length > 0) | |
| { | |
| using (Customers customer = new Customers()) | |
| { | |
| // Setting the ObjectMode on the customer object to ObjectModes.Search | |
| // tells the customer object to change its field's UseInSearch property to true | |
| // when a value is assigned to the field's value property. | |
| customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search; | |
| // MoreResults property indicates weather the object will match all or match any | |
| // Essentially it is equivalent to using AND/OR | |
| customer.MoreResults = !this.CheckBox_MatchBoth.Checked; | |
| // PartialTextMatch property indicates weather the field values specified in the criteria | |
| // must match exactly or partially. This can be overwritten at each individual field as well | |
| // by resetting the PartialTextMatch of the stringField object. | |
| // This property only applies to the fields of "stringField" type | |
| customer.PartialTextMatch = this.CheckBox_PartialTextMatch.Checked; | |
| if (this.TextBox_FirstName.Text.Trim().Length > 0) | |
| { | |
| // Since the user specified the first name, we will assign the user specified | |
| // value to the FirstName field property on the customer object. | |
| // Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search) | |
| // the FirstName.UseInSearch property will be set to true (by default it is set to false). | |
| customer.FirstName.Value = this.TextBox_FirstName.Text; | |
| } | |
| if (this.TextBox_LastName.Text.Trim().Length > 0) | |
| { | |
| // Since the user specified the last name, we will assign the user specified | |
| // value to the LastName field property on the customer object. | |
| // Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search) | |
| // the LastName.UseInSearch property will be set to true (by default it is set to false). | |
| customer.LastName.Value = this.TextBox_LastName.Text; | |
| } | |
| // The find method returns a DataSet, and the DataSet is also available in the ResultSet property | |
| // of the customer object after the Find has been called. | |
| // NOTE: The ResultSet will not contain a DataSet if the AddResultsToDataSet is set to false. | |
| // In such scenario, you must retain a reference to the DataSet returned by the Find method like: | |
| // DataSet myDs = customer.Find(); | |
| customer.Find(); | |
| // The following line ensures that the Find method actually returned a DataTable | |
| if (customer.ResultSet.Tables.Contains(customer.GetResultSetName())) | |
| { | |
| this.GridView.DataSource = customer.ResultSet; | |
| // The GetResultSetName method on the customer object is a very useful method | |
| // as it returns the name of the DataTable that the Find and other methods will use | |
| // to create a DataTable in the ResultSet property. | |
| // Essentially it is same as checking if ResultSetName property is specified then that is used. | |
| // Otherwise, the Name property is used and GetResultSetName provides a short one call to determine that. | |
| this.GridView.DataMember = customer.GetResultSetName(); | |
| } | |
| // ErrorString property is populated with a message anytime there is an exception during any database operation. | |
| else if (customer.ErrorString != null) | |
| { | |
| // Show the Error message returned by the customer object during the database operation. | |
| MessageBox.Show(customer.ErrorString); | |
| } | |
| } | |
| } | |
| ' ensure that user has specified at least one criteria. | |
| If (Me.TextBox_FirstName.Text.Trim().Length > 0 Or Me.TextBox_LastName.Text.Trim().Length > 0) Then | |
| Using customer As Customers = New Customers() | |
| ' Setting the ObjectMode on the customer object to ObjectModes.Search | |
| ' tells the customer object to change its field's UseInSearch property to true | |
| ' when a value is assigned to the field's value property. | |
| customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search | |
| ' MoreResults property indicates weather the object will match all or match any | |
| ' Essentially it is equivalent to using AND/OR | |
| customer.MoreResults = Not Me.CheckBox_MatchBoth.Checked | |
| ' PartialTextMatch property indicates weather the field values specified in the criteria | |
| ' must match exactly or partially. This can be overwritten at each individual field as well | |
| ' by resetting the PartialTextMatch of the stringField object. | |
| ' This property only applies to the fields of "stringField" type | |
| customer.PartialTextMatch = Me.CheckBox_PartialTextMatch.Checked | |
| If Me.TextBox_FirstName.Text.Trim().Length > 0 Then | |
| ' Since the user specified the first name, we will assign the user specified | |
| ' value to the FirstName field property on the customer object. | |
| ' Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search) | |
| ' the FirstName.UseInSearch property will be set to true (by default it is set to false). | |
| customer.FirstName.Value = Me.TextBox_FirstName.Text | |
| End If | |
| If Me.TextBox_LastName.Text.Trim().Length > 0 Then | |
| ' Since the user specified the last name, we will assign the user specified | |
| ' value to the LastName field property on the customer object. | |
| ' Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search) | |
| ' the LastName.UseInSearch property will be set to true (by default it is set to false). | |
| customer.LastName.Value = Me.TextBox_LastName.Text | |
| End If | |
| ' The find method returns a DataSet, and the DataSet is also available in the ResultSet property | |
| ' of the customer object after the Find has been called. | |
| ' NOTE: The ResultSet will not contain a DataSet if the AddResultsToDataSet is set to false. | |
| ' In such scenario, you must retain a reference to the DataSet returned by the Find method like: | |
| ' DataSet myDs = customer.Find(); | |
| customer.Find() | |
| ' The following line ensures that the Find method actually returned a DataTable | |
| If customer.ResultSet.Tables.Contains(customer.GetResultSetName()) Then | |
| Me.GridView.DataSource = customer.ResultSet | |
| ' The GetResultSetName method on the customer object is a very useful method | |
| ' as it returns the name of the DataTable that the Find and other methods will use | |
| ' to create a DataTable in the ResultSet property. | |
| ' Essentially it is same as checking if ResultSetName property is specified then that is used. | |
| ' Otherwise, the Name property is used and GetResultSetName provides a short one call to determine that. | |
| Me.GridView.DataMember = customer.GetResultSetName() | |
| ElseIf (customer.ErrorString Is Nothing) Then | |
| ' ErrorString property is populated with a message anytime there is an exception during any database operation. | |
| ' Show the Error message returned by the customer object during the database operation. | |
| MessageBox.Show(customer.ErrorString) | |
| End If | |
| End Using | |
| End If | |

Click to enlarge