什么是函数执行顺序?

时间:2014-07-14 21:31:51

标签: c# asp.net function

请记住,我是VB背景并切换到C#。

在这段代码中,我只是使用函数将数据插入db,发送电子邮件确认,最后重置所有表单字段。但是,由于ResetForm函数清除了SendEmail函数引用的电子邮件地址字段,因此会生成异常。

在ResetForm启动之前,SendEmail函数不应该完全执行吗?我在这里错过了什么吗?如果我注释清除emailaddress文本框的行,代码就可以工作。

跟踪信息:

“出现错误:参数'address'不能为空字符串。 参数名称:地址“

    protected void btnSendRequest_Click(object sender, EventArgs e)
    {
        try
        {

            InsertRecords();
            SendEmail();

            //If both insert records and send email are successful, clear the form and session variables
            ResetForm();
            confirmation.Style["visibility"] = "visible";
        }
        catch (Exception ex)
        {
            Trace.Warn("There was an error:  " + ex.Message);
        }

     }

    protected void SendEmail()
    {
        DataTable tblStudents = (DataTable)Session["tblstudents"];

        string strSenderEmailAddress = txtEmail.Text;

        MailMessage emlMessage = new MailMessage();
        emlMessage.From = new MailAddress(strSenderEmailAddress);

        emlMessage.To.Add("someaddress@somewhere.com");

        emlMessage.Subject = "Some subject";
        string strMessageBody = "Some wonderful message";
        string strStudentInfo=null;
        foreach (DataRow row in tblStudents.Rows)
        {
            strStudentInfo = strStudentInfo
                + row["firstname"].ToString() + " " + row["middlename"].ToString() + " " + row["lastname"].ToString() + " DOB:  " + row["dob"].ToString() + " " + row["school"].ToString()
                + Environment.NewLine + Environment.NewLine;
        }

        string strMessageFooter = Environment.NewLine + Environment.NewLine + Environment.NewLine + Environment.NewLine

            + "Some info about organization";

        emlMessage.Body = strMessageBody + strStudentInfo + strMessageFooter;

        SmtpClient myClient = new SmtpClient("smtphost");
        myClient.Send(emlMessage);

    }

错误信息:

Line 197:
Line 198:            MailMessage emlMessage = new MailMessage();
Line 199:            emlMessage.From = new MailAddress(strSenderEmailAddress);
Line 200:            //emlMessage.To.Add("someone@else.me");
Line 201:            emlMessage.To.Add("email@email.com");


Source File: c:\Users\igainey\Documents\Visual Studio 2012\Projects    \parentportalsignup    \parentportalsignup\default.aspx.cs    Line: 199

Stack Trace:


[ArgumentException: The parameter 'address' cannot be an empty string.
Parameter name: address]

   System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding     displayNameEncoding) +1475149

   System.Net.Mail.MailAddress..ctor(String address) +8

   parentportalsignup._default.SendEmail() in c:\Users\igainey\Documents\Visual Studio 2012\Projects\parentportalsignup\parentportalsignup\default.aspx.cs:199

   parentportalsignup._default.btnSendRequest_Click(Object sender, EventArgs e) in c:\Users\igainey\Documents\Visual Studio 2012\Projects\parentportalsignup\parentportalsignup\default.aspx.cs:240

   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9752554

   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +196

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10

   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String     eventArgument) +13

   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35

   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

0 个答案:

没有答案