views:

74

answers:

3

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  A: 

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.

Michael Petrotta
+1  A: 

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

Merrimack
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();
Ritesh Ranjan
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.
Merrimack
A: 

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.

bgever
hi bgever,Please provide the sample code i am using oracle as datasbase.plz do the needful.
Ritesh Ranjan