title: The name 'sb' does not exist in the current context - .net intermittent error not receiving from c#
body: <p>I have a .net file that utilizes a c# to process some form(post) information, disect it, do calculations and create a string to print on screen, and send in an email. </p>
  
  <p>I am getting the following error, however I am only getting it at different times.  The page will work for an hour(or other random amount of time), and then will not work for a period of time.</p>
  
  <p><em>From my .aspx file</em></p>
  
  <p><strong>Compiler Error Message: CS0103: The name 'sb' does not exist in the current context</strong></p>
  
  <pre><code>Line 86:        &lt;% Response.Write(sb); %&gt;
  </code></pre>
  
  <blockquote>
    <p><strong>Here is the code layout.</strong>  I am including, even though I don't think
    there is an issue with the code, because it is working sometimes and
    other times not.  It's as if something just has a brain lapse.  I've looked for files to delete and can't find anything.  This is my first attempt with c#.  We use it at my place of work and I've seen the code quite a bit, but haven't attempted to play with it on my own.</p>
  </blockquote>
  
  <p>*beginning of grade_the_quiz.aspx*</p>
  
  <pre><code>&lt;%@ Page Language="C#" autoeventwireup="true" CodeFile="automail.aspx.cs" inherits="formsucker" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
  </code></pre>
  
  <p>end head / begin site layout  (removed for simplicity)</p>
  
  <pre><code>&lt;div id="contentleft"&gt;
  &lt;h1&gt;How do you Compare?&lt;/h1&gt;
  &lt;p&gt;Compare yourself to your competition - how do you measure up against the best practices benchmark of the most successful Senior Living Caregivers in America?*&lt;/P&gt;
      &lt;form id="form1" runat="server"&gt;
      &lt;div id="grade"&gt;        
          &lt;% Response.Write(sb); %&gt;
  &lt;/div&gt;         
  &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>(etc, end html, #EOF removed for simplicity)</p>
  
  <p><em>beginning of automail.aspx.cs</em></p>
  
  <pre><code>using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.IO;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System.Net.Mail;
  using System.Net;
  using System.Text;
  
  
  public partial class formsucker : System.Web.UI.Page
  {
  
  
      public void Page_Init(object sender, EventArgs e)
      {
              string reTake = "", answerScore = "", pleaseWork = "", firstname = "", email = "";
              int noAnswer = 0, testVal = 0;
  
          String sdData = Request["sd"];
  
  
          string[] sdValuesSplit = sdData.Split('~');
          foreach (string postValues in sdValuesSplit)
          {
                  if (postValues.Contains("FirstName"))
                  {
                      firstname = postValues.Split('=')[1];
  
                  }
                  if (postValues.Contains("EMail"))
                  {
                      email = postValues.Split('=')[1];
  
                  }
  
                  }
  
  
          StringBuilder sb = new StringBuilder();
  
          sb.Append("&lt;p&gt;Below are your results to the quiz " + firstname + ", and we will email them to " + email + " right away!&lt;/p&gt;");
  
             switch (Request["CUSTOM_FORM_FIELD_11111"])
             {
                 case "Yes":
                     sb.Append("&lt;p&gt;&lt;b&gt;Do you have a system to track your notes, history and activity?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;li&gt;&lt;i&gt;YES&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;li&gt; Congratulations!  You are ahead of many!  You understand the value in capturing your prospects/clients critical information.  Capturing information about their history, notes and activity allows information to be shared amongst your team.  You are utilizing a power piece of the puzzle when it comes to keeping up to date with your prospects/clients&lt;/li&gt;&lt;/p&gt;");
                     testVal = testVal + 5;
                     break;
                 case "No":
                     sb.Append("&lt;p&gt;&lt;b&gt;Do you have a system to track your notes, history and activity?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;li&gt;&lt;i&gt;NO&lt;/i&gt;&lt;ul&gt;&lt;li&gt; Consider how much time and money it cost to find new leads -- not keeping track of this information can lead to your prospects or clients falling through the cracks.  Marketing to your prospect/client does not work if you don't know what the last activity &lt;/li&gt;&lt;/p&gt;");
                     break;
                 default:
                     sb.Append("&lt;p&gt;&lt;b&gt;Do you have a system to track your notes, history and activity?&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;li&gt;&lt;i&gt;You Didn\'t Answer the Question&lt;/i&gt;&lt;/li&gt;&lt;/p&gt;");
                     noAnswer = noAnswer + 1;
                     break;
             }
  </code></pre>
  
  <p><em>More Switch Statements that append to sb (<strong>about 20 switch statements removed</strong>.. it's grading a quiz and then returning different statements and appending them to sb.</em></p>
  
  <pre><code>  StreamWriter sw = new StreamWriter(@"C:\inetpub\websites\occupancyadvantage.com\mailTest.txt", true);
                          try
                          {
  
                              System.Net.Mail.MailMessage automail = new System.Net.Mail.MailMessage();
                              automail.Subject = "How Do You Compare Quiz Results";
                              automail.To.Add(email);
                              MailAddress address = new MailAddress("XXXXXXXREMOVED", "XXXXXXREMOVED");
                              automail.From = address;
                              automail.IsBodyHtml = true;
                              automail.Body = "&lt;html&gt;&lt;body&gt;&lt;p&gt;" + sb + "&lt;/p&gt;&lt;p&gt;XXXXXREMOVED&lt;/p&gt;" +
                                  "This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited." +
                                  "&lt;br /&gt; XXXXXREMOVED Notifications | 2012&lt;/body&gt;&lt;/html&gt;";
                              SmtpClient autosmtp = new SmtpClient("XXXXXREMOVED", 25);
                              NetworkCredential netCred = new NetworkCredential("XXXXXREMOVED", "XXXXXREMOVED");
                              autosmtp.Credentials = netCred;
                              autosmtp.Send(automail);
  
                              //SmtpMail.SmtpServer = "mail.XXXXXREMOVED.net";
                              //SmtpMail.Send(automail);
  
                              sw.WriteLine("Success:" + DateTime.Now.ToString());
                              sw.Close();
                              sw.Dispose();
  
                          }
                          catch (Exception x)
                          {
                              sw.WriteLine("Failure:" + DateTime.Now.ToString());
                              sw.WriteLine(x.ToString() + ":" + x.StackTrace.ToString());
                              sw.Close();
                              sw.Dispose();
                          }
  
  
  
  
          }
      }
  </code></pre>
  
  <p>If you have gotten this far, I hope I have put the most important information.  I've searched through 20 or so of these and found different ways that this happens, just can't find out my solution.  I have created everything in notepad ++  The original site with everything going on was in VS.  </p>