Quick Objects Documentation Akal Tech Logo
Performing Complex Searches

Glossary Item Box

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:

1 // ensure that user has specified at least one criteria.  
2 if (this.TextBox_FirstName.Text.Trim().Length > 0 || this.TextBox_LastName.Text.Trim().Length > 0)  
3 {  
4     using (Customers customer = new Customers())  
5     {  
6         // Setting the ObjectMode on the customer object to ObjectModes.Search  
7         // tells the customer object to change its field's UseInSearch property to true  
8         // when a value is assigned to the field's value property.  
9         customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search;  
10           
11         // MoreResults property indicates weather the object will match all or match any  
12         // Essentially it is equivalent to using AND/OR  
13         customer.MoreResults = !this.CheckBox_MatchBoth.Checked;  
14           
15         // PartialTextMatch property indicates weather the field values specified in the criteria  
16         // must match exactly or partially. This can be overwritten at each individual field as well  
17         // by resetting the PartialTextMatch of the stringField object.  
18         // This property only applies to the fields of "stringField" type  
19         customer.PartialTextMatch = this.CheckBox_PartialTextMatch.Checked;  
20         if (this.TextBox_FirstName.Text.Trim().Length > 0)  
21         {  
22             // Since the user specified the first name, we will assign the user specified  
23             // value to the FirstName field property on the customer object.  
24             // Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search)  
25             // the FirstName.UseInSearch property will be set to true (by default it is set to false).  
26             customer.FirstName.Value = this.TextBox_FirstName.Text;  
27         }  
28         if (this.TextBox_LastName.Text.Trim().Length > 0)  
29         {  
30             // Since the user specified the last name, we will assign the user specified  
31             // value to the LastName field property on the customer object.  
32             // Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search)  
33             // the LastName.UseInSearch property will be set to true (by default it is set to false).  
34             customer.LastName.Value = this.TextBox_LastName.Text;  
35         }  
36  
37         // The find method returns a DataSet, and the DataSet is also available in the ResultSet property  
38         // of the customer object after the Find has been called.  
39         // NOTE: The ResultSet will not contain a DataSet if the AddResultsToDataSet is set to false.  
40         //       In such scenario, you must retain a reference to the DataSet returned by the Find method like:  
41         //       DataSet myDs = customer.Find();  
42         customer.Find();  
43           
44         // The following line ensures that the Find method actually returned a DataTable  
45         if (customer.ResultSet.Tables.Contains(customer.GetResultSetName()))  
46         {  
47             this.GridView.DataSource = customer.ResultSet;  
48             // The GetResultSetName method on the customer object is a very useful method  
49             // as it returns the name of the DataTable that the Find and other methods will use  
50             // to create a DataTable in the ResultSet property.  
51             // Essentially it is same as checking if ResultSetName property is specified then that is used.  
52             // Otherwise, the Name property is used and GetResultSetName provides a short one call to determine that.  
53             this.GridView.DataMember = customer.GetResultSetName();  
54         }  
55         // ErrorString property is populated with a message anytime there is an exception during any database operation.  
56         else if (customer.ErrorString != null)  
57         {  
58             // Show the Error message returned by the customer object during the database operation.  
59             MessageBox.Show(customer.ErrorString);  
60         }  
61     }  
62 }  
63  
1 ' ensure that user has specified at least one criteria.  
2 If (Me.TextBox_FirstName.Text.Trim().Length > 0 Or Me.TextBox_LastName.Text.Trim().Length > 0) Then 
3     Using customer As Customers = New Customers()  
4         ' Setting the ObjectMode on the customer object to ObjectModes.Search  
5         ' tells the customer object to change its field's UseInSearch property to true  
6         ' when a value is assigned to the field's value property.  
7         customer.ObjectMode = Akal.QuickObjects.ObjectBase.ObjectModes.Search  
8  
9         ' MoreResults property indicates weather the object will match all or match any  
10         ' Essentially it is equivalent to using AND/OR  
11         customer.MoreResults = Not Me.CheckBox_MatchBoth.Checked  
12  
13         ' PartialTextMatch property indicates weather the field values specified in the criteria  
14         ' must match exactly or partially. This can be overwritten at each individual field as well  
15         ' by resetting the PartialTextMatch of the stringField object.  
16         ' This property only applies to the fields of "stringField" type  
17         customer.PartialTextMatch = Me.CheckBox_PartialTextMatch.Checked  
18         If Me.TextBox_FirstName.Text.Trim().Length > 0 Then 
19             ' Since the user specified the first name, we will assign the user specified  
20             ' value to the FirstName field property on the customer object.  
21             ' Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search)  
22             ' the FirstName.UseInSearch property will be set to true (by default it is set to false).  
23             customer.FirstName.Value = Me.TextBox_FirstName.Text  
24         End If 
25         If Me.TextBox_LastName.Text.Trim().Length > 0 Then 
26             ' Since the user specified the last name, we will assign the user specified  
27             ' value to the LastName field property on the customer object.  
28             ' Since the customer object is in the Search Mode (ObjectMode == ObjectModes.Search)  
29             ' the LastName.UseInSearch property will be set to true (by default it is set to false).  
30             customer.LastName.Value = Me.TextBox_LastName.Text  
31         End If 
32  
33         ' The find method returns a DataSet, and the DataSet is also available in the ResultSet property  
34         ' of the customer object after the Find has been called.  
35         ' NOTE: The ResultSet will not contain a DataSet if the AddResultsToDataSet is set to false.  
36         '       In such scenario, you must retain a reference to the DataSet returned by the Find method like:  
37         '       DataSet myDs = customer.Find();  
38         customer.Find()  
39  
40         ' The following line ensures that the Find method actually returned a DataTable  
41         If customer.ResultSet.Tables.Contains(customer.GetResultSetName()) Then 
42             Me.GridView.DataSource = customer.ResultSet  
43             ' The GetResultSetName method on the customer object is a very useful method  
44             ' as it returns the name of the DataTable that the Find and other methods will use  
45             ' to create a DataTable in the ResultSet property.  
46             ' Essentially it is same as checking if ResultSetName property is specified then that is used.  
47             ' Otherwise, the Name property is used and GetResultSetName provides a short one call to determine that.  
48             Me.GridView.DataMember = customer.GetResultSetName()  
49         ElseIf (customer.ErrorString Is NothingThen 
50             ' ErrorString property is populated with a message anytime there is an exception during any database operation.  
51             ' Show the Error message returned by the customer object during the database operation.  
52             MessageBox.Show(customer.ErrorString)  
53         End If 
54     End Using  
55 End If 
56