1

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.

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.

From my .aspx file

Compiler Error Message: CS0103: The name 'sb' does not exist in the current context

Line 86:        <% Response.Write(sb); %>

Here is the code layout. 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.

*beginning of grade_the_quiz.aspx*

<%@ Page Language="C#" autoeventwireup="true" CodeFile="automail.aspx.cs" inherits="formsucker" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

end head / begin site layout (removed for simplicity)

<div id="contentleft">
<h1>How do you Compare?</h1>
<p>Compare yourself to your competition - how do you measure up against the best practices benchmark of the most successful Senior Living Caregivers in America?*</P>
    <form id="form1" runat="server">
    <div id="grade">        
        <% Response.Write(sb); %>
</div>         
</form>
</div>

(etc, end html, #EOF removed for simplicity)

beginning of automail.aspx.cs

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("<p>Below are your results to the quiz " + firstname + ", and we will email them to " + email + " right away!</p>");

           switch (Request["CUSTOM_FORM_FIELD_11111"])
           {
               case "Yes":
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>YES</i></p><p><li> 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</li></p>");
                   testVal = testVal + 5;
                   break;
               case "No":
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>NO</i><ul><li> 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 </li></p>");
                   break;
               default:
                   sb.Append("<p><b>Do you have a system to track your notes, history and activity?</b></p><p><li><i>You Didn\'t Answer the Question</i></li></p>");
                   noAnswer = noAnswer + 1;
                   break;
           }

More Switch Statements that append to sb (about 20 switch statements removed.. it's grading a quiz and then returning different statements and appending them to sb.

  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 = "<html><body><p>" + sb + "</p><p>XXXXXREMOVED</p>" +
                                "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." +
                                "<br /> XXXXXREMOVED Notifications | 2012</body></html>";
                            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();
                        }




        }
    }

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.

4

3 に答える 3

2

簡単にするために、常にクラス レベルの文字列を宣言し、Stringbuilder をそのままにして、すべての結果がオブジェクトに追加された後、Stringbuilder の値をクラス レベルの変数に割り当てることができます。このようにして、Stringbuilder をそのまま保持しますが、ページがロードされて init オブジェクトが期限切れになった後に残る情報へのアクセスを許可します。

 public partial class formsucker : System.Web.UI.Page
{
    public String sbG = "";

    protected void Page_Init(object sender, EventArgs e)
    {
            StringBuilder sb = new StringBuilder(); 
            sb.Append("<p>" + allYourAppends + "</p>");
            sbG = sb.ToString();
    }
}
于 2012-07-11T17:52:43.917 に答える
0

StringBuilderオブジェクトは、実行がPage_Initを通過すると期限切れになります。イベント本体ではなく、クラスで宣言すれば機能します。

このような:

 public partial class formsucker : System.Web.UI.Page
 {
     public StringBuilder sb = new StringBuilder();
     public void Page_Init(object sender, EventArgs e)
     {
        string reTake = "", answerScore = "", pleaseWork = "", firstname = "", email = "";
        int noAnswer = 0, testVal = 0;
        //blah blah...
     }
 }

また、86行目:<%Response.Write(sb); %>は次のようになります:<%Response.Write(sb.ToString()); %>

于 2012-07-11T17:12:41.337 に答える
0

「sb」のスコープは Page_Init に限定されます。それを formsucker クラスのスコープに移動すると、エラーは解消されます (はずです)。

最も可能性が高いのは、sb がガベージ コレクションされる前に aspx によってアクセスされていたことです。ガベージ コレクションはさまざまなタイミングで行われるため、断続的である可能性があります。

于 2012-07-11T17:04:46.603 に答える