0

Dear, I am facing system.outofmemmory exception when binding a gridview with a datatable having more then 400000 records please find the below sample code for the same

GridView gv = new GridView();
this.EnableViewState = false;
gv.DataSource = (DataTable)dt;
gv.DataBind();

Kindly help me to overcome in this situation is there any limitation of gridview for databind?

2
  • 6
    Who wants to look at a gridview with 400000 records? Commented Sep 28, 2010 at 5:38
  • 1
    Even if you have a server with infinite memory, you should not return 400000 records from a query. :S Why not paging on the query? [blog.sqlauthority.com/2007/04/03/… Commented Sep 28, 2010 at 5:42

3 Answers 3

2

A datagrid is limited by available system memory, and you're likely running into that. Assuming a process address space of, say, 1.5GB, that's about 4KB per datagrid row, if you had nothing else consuming memory in your process. 4KB is not unreasonable, assuming a dozen or so wide columns.

You're trying to display too many rows at once. I have to assume it's a pretty bad user experience too, trying to navigate through that many rows. Look at paging them instead.

Sign up to request clarification or add additional context in comments.

Comments

2

As suggested already, paging is the common way to handle large amounts of records in web apps.

But if it is the requirement to show all records in the output (e.g. for reporting purposes) then there is a way to do it.

Instead of keeping the resultset in memory on the server, you can also stream the data to the client. This can be achieved by using a DataReader instead of a DataSet and disabling all caching mechanisms.

A simple way to do this is using a SqlDataSource control for instance. Hook up the data source to a grid view as usual. Then change the DataSourceMode on the data source from DataSet to DataReader. Disable the page output cache, and you are ready to go.

Comments

1

That's a lot of rows! You need to apply paging for that amount of rows to be manageable. If the reason you are displaying that many rows is that you need to find certain data you should implement a search function as well.

Here's a guide from MS on how to enable paging: http://msdn.microsoft.com/en-us/library/aa479347.aspx

2 Comments

Dear, i am not using that grid view to display the record i am using it for exporting the data into excel having 10 lacks record,sample code.Response.ContentType = "application/vnd.ms-excel";string FileName = "PoliciesDetailsForBranch";Response.AppendHeader("content-disposition", "attachment; filename=" + FileName + ".xls");GridView gv = new GridView();this.EnableViewState = false;gv.DataSource = (DataTable)dt;gv.DataBind();this.ClearControls(gv);System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(Response.Output);gv.RenderControl(hw);Response.End();
Then you shouldn't use DataGrid at all. One alternative is to use the HtmlTextWriter and write the output yourself, row by row. But you should really consider why you are doing this. 400.000 rows is still a lot and you will likely face timeouts later when fetching the data. It sounds as though you are returning all known data? If so, you should really create an export function in your SQL-database that dump the data to an excel file and just return the existing file through your service/page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.