OutofMemory exception in the application
<p>in the application log files this is the error we are getting </p>

<blockquote>
  <p>2012-07-19 15:08:46,995 [17] WARN  System.Web.UI.Page [(null)] -
  Logging:System.OutOfMemoryException: Exception of type
  'System.OutOfMemoryException' was thrown.    at
  System.String.GetStringForStringBuilder(String value, Int32
  startIndex, Int32 length, Int32 capacity)    at
  System.Text.StringBuilder.GetNewString(String currentString, Int32
  requiredLength)    at System.Text.StringBuilder.Append(String value)</p>
</blockquote>

<p>can any one suggest how to avoid this problem. we have used string builder for writing the data into the excel sheet </p>

<p>here is the code</p>

<pre><code>public void ToCSV(DataTable dt)
{
    StringBuilder sb = null;
    try
    {

        sb = new StringBuilder(50*1024);
        foreach (DataColumn col in dt.Columns)
        {
            sb.Append(col.ColumnName + ",");
        }
        sb.Remove(sb.Length - 1, 1);
        sb.Append(Environment.NewLine);
        foreach (DataRow row in dt.Rows)
        {
            for (int i = 0; i &lt; dt.Columns.Count; i++)
            {
                sb.Append("\"" + row[i].ToString() + "\"" + ",");
            }
            sb.Append(Environment.NewLine);
        }
        Session["exportsCsv"] = sb;
        sb = null;


    }
    catch (Exception ex)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Warn("Logging:" + ex);

    }
    finally {
        sb = null;

    }
    //
</code></pre>
