Select to view content in your preferred language

Database search filter error handling

510
2
08-12-2010 10:19 AM
JoshuaPersson
Occasional Contributor
Hello-

...I have written some code for a dockwindow where the user enters a name in a textbox which is then passed to a variable. The variable is then used in a search filter which searches the "Name" field from a feature class in a File Geodatabase- which works fine until it comes across a <null> field or an empty string. What I need to do is include a way to catch these errors and prompt the user before the search is executed...any thoughts? Part of my code is below:


        'Gets search criteria from text box
        Dim namesearch As String
        namesearch = TextBox1.Text

        'Gets the table associated with the layer.
        Dim mapDisplay As MapDisplay = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay
        Dim myMap As Map = MapDisplay.Map

        Dim findName As Table = DirectCast(myMap.FindByName("Lots"), FeatureLayer).Table

        ' The Search method takes a Filter object, which has the SQL query defined in its constructor.
        Dim searchName As RowCollection = findName.Search(New Filter("Name LIKE '%" + namesearch  + "%';", New String() {"Name", "Lot_Section", "OBJECTID"}))
           
           ' RowCollection gets passed to the DataGrid
            Dim tba As TableBindingAdapter = New TableBindingAdapter(searchName)
            tba.Fill()

                          Thanks,

                                  -Josh
0 Kudos
2 Replies
MichaelBranscomb
Esri Frequent Contributor
Joshua,

I would usually write some code to verify the user input first... so for a string input such as name I would probably do the following (examples in C#):

1. Check that your Textbox.Text is not "" (empty)

e.g. if(myTextBox.Text == "")
          return;

OR

e.g. if(myTextBox.Text == "")
          throw New Exception("Please enter some text");

2. Check that your Textbox.Text is likely to be a valid name string (double has a TryParse() method which returns true if the input string can be cast to a number)

See http://msdn.microsoft.com/en-us/library/3s27fasw.aspx

e.g.

double tempDouble;
if(double.TryParse(myTextBox.Text,out tempDouble)
    throw New Exception("You entered a number");

3. Perhaps check that your Textbox.Text is not too short or too long

e.g. if(myTextBox.Text.Length < 2 | myTextBox.Text > 100)
          throw New Exception("Please check the name and try again");

Then because I'm throwing Exceptions above I'd put the whole lot in a Try/Catch statement (in C#):

e.g.

Try
{
... (code above)
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

Testing the input string like that means you can test possible erroneous scenarios before submitting the query.

When you say <null> field or empty string do you mean in the dataset returned from the query?

I hope that helps some.

Cheers

Mike
0 Kudos
MikeRudden
Emerging Contributor
Hi,

Could the problem be with your Filter arguments - I can't see an overload that takes a string, string, string[], string? The closest overload to this one expects a Geometry as the first argument.

If you want check for null values you can do this by incorporating an IS NULL check into your expression, for example:

RowCollection rows = country.Search(new Filter("LINK IS NULL"));

There's more information on performing searches in the Explorer SDK doc, How to Search a Table


Regards
Mike Rudden
ESRI
0 Kudos