Memory error

Hi Experts,

Below is the code. I have 3 tables, the second table contains 100,000 rows ,its afiling at RenderControl with the
execption : Exception of type System.OutOfMemoryException was thrown.
Please advise.
foreach (Table table in tables)
{
StringWriter sw = new StringWriter(output);
HtmlTextWriter htw = new HtmlTextWriter(sw);
table.RenderControl(htw);
}

Open in new window

BkuniyilAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
>> the second table contains 100,000 rows
assuming each rows consumes 2Kb in unicode, we are talking of about 200,000,000 = 200 Mb of text. But during html construction, maybe you will need even more memory, maybe up to 1 GB, this is too much heavy for any standard computer. I think it is no sense to try to do this in memory, you have to render directly to a file stream.
0
 
McExpCommented:
What actually is the code trying to do?
0
 
BkuniyilAuthor Commented:
Will finally save to a file
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Jaime OlivaresSoftware ArchitectCommented:
you have to write directly to file. Associate your writer with a FileStream.
0
 
McExpCommented:
What he said!
0
 
Jaime OlivaresSoftware ArchitectCommented:
try with this:

TextWriter sw =  new StreamWriter("somefile.html");
HtmlTextWriter htw = new HtmlTextWriter(sw);
 
foreach (Table table in tables)
{
     table.RenderControl(htw);
}
htw.Close();

Open in new window

0
 
BkuniyilAuthor Commented:
The above code still saves only part of the data to the file "somefile.html"
0
 
Jaime OlivaresSoftware ArchitectCommented:
what do you mean with "part of the data" ?
0
 
Jaime OlivaresSoftware ArchitectCommented:
try to use Flush:
TextWriter sw =  new StreamWriter("somefile.html");
HtmlTextWriter htw = new HtmlTextWriter(sw);
 
foreach (Table table in tables)
{
     table.RenderControl(htw);
     htw.Flush();
}
htw.Close();
0
 
BkuniyilAuthor Commented:
I need to write the loop through each of the tables and write it to the StringBuilder object. Is there any way of doing it without writing it to a file?
StringBuilder output = new StringBuilder();
foreach (Table table in tables)
{
StringWriter sw = new StringWriter(output);
HtmlTextWriter htw = new HtmlTextWriter(sw);
table.RenderControl(htw);
}

Open in new window

0
 
BkuniyilAuthor Commented:
Is there a maximum value that a StringBuilder Object can hold, i get an Out of Memory error.Appreciate all help.
0
 
Jaime OlivaresSoftware ArchitectCommented:
capacity of a StringBuilder class is 4GB.
0
 
BkuniyilAuthor Commented:
The file i am trying to build through the Stringbuilder object is  2MB file, it should work alright?
0
 
Jaime OlivaresSoftware ArchitectCommented:
Yes, it should, unless something bad in your loop
0
 
BkuniyilAuthor Commented:
The code in the loop is pasted above.Please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.