Link to home
Start Free TrialLog in
Avatar of Bkuniyil
Bkuniyil

asked on

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

Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

What actually is the code trying to do?
Avatar of Bkuniyil
Bkuniyil

ASKER

Will finally save to a file
you have to write directly to file. Associate your writer with a FileStream.
What he said!
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

The above code still saves only part of the data to the file "somefile.html"
what do you mean with "part of the data" ?
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();
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

Is there a maximum value that a StringBuilder Object can hold, i get an Out of Memory error.Appreciate all help.
capacity of a StringBuilder class is 4GB.
The file i am trying to build through the Stringbuilder object is  2MB file, it should work alright?
Yes, it should, unless something bad in your loop
The code in the loop is pasted above.Please advise
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial