Click here to Skip to main content
12,894,899 members (57,470 online)
Rate this:
Please Sign up or sign in to vote.
See more: ADO.NET
Hello Everybody,

I have the following problem.

I have a large database table (inside a MS Sql-database) which I read inside a DataTable object. The DataTable object itself is bound to a DataGridView object where I show the data. Sometimes I would like to hide some data, which is not relevant for the current view. The DataView object inside the DataTable seems to be the best way to do this. DataView has a property RowFilter where you can set the visible rows. The input to RowFilter is a sql string. Unfortunately, I get a stack overflow exception when the string becomes too long. The following happens when then:

“An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll”

I have already tried to split the string and call RowFilter several times but this would override the old settings. The same happens when calling "Select" function from the DataTable object.

Do someone have a similar problem and find a solution.

Below, I have scheduled my code:

Thank you for all help


DataTable sourceTable = new DataTable(); 
SqlStringBuilder filter = new SqlStringBuilder();
// Set source from database
// Fill "filter" with a very large string
// Like {[intType = '1']} AND(intFamilyID = '2' OR intFamilyID = '4' OR 
   intFamilyID = '6') .........................} 
// 17 000 signs
// Here happens the problem
sourceTable.DefaultView.RowFilter = = filter.ToString();
// The same happens when trying this. 
DataRow[] resultSet = sourceTable.Select(filter.ToString());
Posted 17-Sep-11 22:18pm
Updated 17-Sep-11 22:41pm
Mehdi Gholam334.6K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Its better to filter your query on the database side then show the results on your grid.

The database engine is designed to do this more efficiently.

Keep the RowFilter for simple filters once the results are back.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Don't do it that way at all - filter the rows coming from the database at source, instead of later. If you have a large database, then returning all the rows and filtering out those you dont want to display is wasteful of bandwidth, memory, and database server resources. It will also slow the display for the user.

Never return rows or fields you are not going to use, and never return more than a hundred or so rows if you can help it - think of the poor user wading through that amount of data!

In addition, it will probably get rid of your problem...:laugh:
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I have little bit recover the issue by using 'IN' in my row filter expression.
I have tested and found no issue in-case the length of RowFilter expression is approx 75K.

Try by using 'IN' operator, hope it works for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 26 Jun 2015
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100