?
Solved

DataView RowFilter raises the IndexOutOfRangeException exception ... how to avoid this error?

Posted on 2007-07-25
18
Medium Priority
?
541 Views
Last Modified: 2011-10-03
I am using DataView.RowFilter to filter a DataTable, but when there are no results in the filter, an IndexOutOfRangeException is raised.  This seems to be normal .NET behavior, from what I am seeing on the web.  It is perfectly normal for my filter results to have 0 matches, and I don't want to see this error message whenever no results are found.  Here is my line of code:

      dv.RowFilter = "PositionID like '" + PositionNumber + "%'";

I dislike this solution, but I was going to put a try/catch around this line and just swallow the error, but the try/catch does NOT catch the error!  I read online that an event is raised, and the error occurs in this event, and that's why the try/catch doesn't work.  Someone else recommended I put a try/catch around the code in my Main program, but doesn't doesn't catch it, either.

Can someone recommend a way to handle this error?  A simple code example would be nice.  I'd prefer to handle it without a try/catch, but I'll take any solution that works!

Thanks,
Brian
0
Comment
Question by:bek
  • 8
  • 7
  • 3
18 Comments
 
LVL 6

Expert Comment

by:PHD
ID: 19567822
try
{
  dv.RowFilter = "PositionID like '" + PositionNumber + "%'";
}
catch (Exception error)
{

}
0
 
LVL 6

Expert Comment

by:PHD
ID: 19567833
I don't think that when the rowfilter return 0 rows this raise an error.
Are you sure about the source of your error ?
0
 
LVL 6

Expert Comment

by:PHD
ID: 19567851
Your error is when you bind the dataview to a control ?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 19568009
There is something else going on here, because if you have a DataView with Count = 0, and you bind it to a DataList/GridView, with 2005, then there isn't an error.  Maybe this happens with 2003/1.1.

Bob
0
 

Author Comment

by:bek
ID: 19568536
Thanks for responding guys.  No, nothing else going on.  There are a lot of people having this same problem.  Here is just one thread about it:

http://groups.google.com/group/microsoft.public.dotnet.framework.adonet/browse_thread/thread/1fad5c27d734934b/052e2c62d66afe9f?lnk=st&q=c%23+rowfilter+indexoutofrangeexception&rnum=2&hl=en#052e2c62d66afe9f

This is exactly what I am encountering.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19568567
That is an example of using the DataGrid, which is a 2003 control.  I don't have 2003, so I don't have that problem.

Bob

0
 

Author Comment

by:bek
ID: 19568576
Sorry, I should have mentioned I am binding to a listbox.  I think that is part of the problem.

I am thinking about unbinding before performing the RowFilter, then checking the result, and if the result is > 0 then rebinding.  Not a great solution, but perhaps that will suffice.
0
 

Author Comment

by:bek
ID: 19568581
I am using VS2005.
0
 

Author Comment

by:bek
ID: 19568647
Also, I don't have any problems when binding.  It's only when I use the RowFilter and there are no matches for the filter expression.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19568707
Is this a Windows Form ListBox or ASP.NET?

Bob
0
 

Author Comment

by:bek
ID: 19568928
Windows Forms
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19569007
I ran a test:

    Dim dt As DataTable = FillDataTable()

    dt.DefaultView.RowFilter = "City Like 'Bston%' "

    Me.ListBox1.DataSource = dt.DefaultView
    Me.ListBox1.DisplayMember = "City"

No exception!!

Bob
0
 

Author Comment

by:bek
ID: 19569036
There is no DataView in your test. :(
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19569136
dt.DefaultView is a DataView.

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 19569210
This doesn't produce an exception either:

            DataTable dt = FillDataTable();

            DataView dv = new DataView(dt);
            dv.RowFilter = "City Like 'Bston%' ";

            this.listBox1.DataSource = dv;
            this.listBox1.DisplayMember = "City";

I just get an empty ListBox.

Bob
0
 

Author Comment

by:bek
ID: 19569222
Interesting.  Do you have all exceptions turned on in your IDE?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 19569350
Yep, no exceptions!!  So, maybe the problem lies in how you are implementing this.

Bob
0
 

Author Comment

by:bek
ID: 19569535
Hmm... could be, I guess.

I hate this solution, but I finally got it to work by using this workaround code:

private void FindEmployeeByPositionNumber (string PositionNumber) {

    DataView tempview = new DataView (dt);
    string tempfilter = dv.RowFilter;
    tempview.RowFilter = "PositionID like '" + PositionNumber + "%'";

    if (tempview.Count > 0) {
        dv = tempview;
        BindListBox (dv);
    }
    lblNumEmployees.Text = dv.Count + " Employees";
}

Thanks for eliminating that issue with the filter.
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question