即使详细信息视图插入失败,应用程序也会发送电子邮件

时间:2015-11-10 18:37:00

标签: c# asp.net detailsview insert-into

我有一个附加到sql数据源的详细信息视图。当插入新的工作订单时,我正在发送电子邮件。目前我的程序存在一些问题,用户无法从我的应用程序中插入数据,但假设数据已插入,电子邮件仍会发送。

这是我的详细信息视图插入方法:

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
 if (successfull == true && Page.IsValid && e.AffectedRows ==1)
        {

            //TextBox TextBoxWorkOrderNumber = (TextBox)(DetailsView1.FindControl("TextBox11"));
            TextBox TextBoxRequestor = (TextBox)(DetailsView1.FindControl("TextBox3"));
            TextBox TextBoxDate = (TextBox)(DetailsView1.FindControl("TextBox1"));
            //TextBoxDate.Text = DateTime.Now.ToShortDateString();
            TextBox TextBoxDepartment = (TextBox)(DetailsView1.FindControl("TextBox4"));
            TextBox TextBoxCompletionDate = (TextBox)(DetailsView1.FindControl("TextBox16"));
            TextBox TextBoxMachineDescription = (TextBox)(DetailsView1.FindControl("TextBox5"));
            TextBox TextBoxMachineLocation = (TextBox)(DetailsView1.FindControl("TextBox6"));
            TextBox TextBoxWorkRequired = (TextBox)(DetailsView1.FindControl("TextBox7"));
            // DropDownList status = (DropDownList)(DetailsView1.FindControl("DropDownList2"));
            TextBox TextBoxStatus = (TextBox)(DetailsView1.FindControl("TextBox12"));
            TextBoxStatus.Text = "Open";






            DropDownList list = (DropDownList)(DetailsView1.FindControl("DropDownList1"));
            TextBox9.Text = list.SelectedValue;
            DropDownList lists = (DropDownList)(DetailsView1.FindControl("DropDownList2"));
            TextBox14.Text = lists.SelectedValue;


            if (TextBoxRequestor.Text.Length <= 0)
            {
                TextBoxRequestor.Text = "Not Applicable";
            }
            if (TextBox14.Text.Length <= 0)
            {
                TextBoxDepartment.Text = "Not Provided";
            }
            if (TextBoxCompletionDate.Text.Length <= 0)
            {
                TextBoxCompletionDate.Text = "Not Provided";
            }
            if (TextBoxMachineDescription.Text.Length <= 0)
            {
                TextBoxMachineDescription.Text = "Not Provided";
            }

            if (TextBoxMachineLocation.Text.Length <= 0)
            {
                TextBoxMachineLocation.Text = "Not Provided";
            }

            if (TextBoxWorkRequired.Text.Length <= 0)
            {
                TextBoxWorkRequired.Text = "Not Provided";
            }

                if (TextBox9.Text == "Safety" && e.AffectedRows==1)
                {
                  {
                      bool isLocal = HttpContext.Current.Request.IsLocal;
                      if (isLocal == true)
                      {
                          string id = TextBox13.Text.ToString();
                          System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
                          mm.From = new System.Net.Mail.MailAddress("no_reply_workorder@.com");//who send
                          mm.To.Add(new System.Net.Mail.MailAddress("abc@.com"));
                          //abc@abc.com
                          mm.Subject = "WorkOrders Type Safety";
                          mm.Body = "DO NOT REPLY TO THIS EMAIL" + "<br><br/>" + "WorkOrderNumber" 
        + ": &nbsp;" + "<a href=\"http://localhost:49695/SafetyReport.aspx?WorkOrderNum=" + TextBox13.Text + "\">"
         + TextBox13.Text + "</a>" + "<-Click on the Work Order Number For Report"
         + "<br><br/>" + "WorkOrderNumber" + ": &nbsp;" + 
        "<a href=\"http://localhost:49695/Safety.aspx?WorkOrderNum=" + 
        TextBox13.Text + "\">" + TextBox13.Text + "</a>" +
         "<-Click on this Work Order Number To Enter Data" + 
        "<br><br/>" + "Requestor" + ":&nbsp;" + TextBoxRequestor.Text +
         "<br><br/>" + "Date" + ":&nbsp;" + TextBoxDate.Text + 
        "<br><br/>" + "Department" + ":&nbsp;" + TextBox14.Text +
         "<br><br/>" + "Machine Description" + ":&nbsp;" + 
        TextBoxMachineDescription.Text + "<br><br/>" + 
        "Machine Location" + ":&nbsp;" + 
        TextBoxMachineLocation.Text + "<br><br/>" +
         "Work Required" + ":&nbsp;" + TextBoxWorkRequired.Text + "<br><br/>" 
                            mm.IsBodyHtml = true;
                            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                            client.Host = ConfigurationManager.AppSettings["smtpServer"];
                            client.Send(mm);
                            captureuseremail();
                       }
              }
    }
  }    

我看到DetailsView1_Item插入如何检查是否插入sql数据库?如果插入成功,我想将布尔值设置为true,如果为true,则执行Details_View1 Inserted并发送电子邮件,否则取消插入。

我也在使用详细信息视图附带的插入按钮。 如果您感到困惑,请向我索要更多代码,我会非常乐意提供。

请帮助:(

添加了附加代码:

  INSERT INTO Master(Requestor, Date, Department, CompletionDate, MachineDescription, 
MachineLocation, [Type of Work Order], [Work Required], Status) 
VALUES (@Requestor, @Date, @Department, @CompletionDate, 
@MachineDescription, @MachineLocation, @Type_of_Work_Order, 
@Work_Required, @Status); SET @NewId = Scope_Identity()

我刚才有一个bool成功;我在Item_Inserting方法()的末尾设置为true。

当用户在详细信息视图上单击提交时,这只是命令按钮插入,然后代码命中item_inserting获取所有值 Item_Inserting详细信息视图:

protected void DetailsView1_ItemInserting(object sender,DetailsViewInsertEventArgs e)         {

        if (Page.IsValid)
        {

            //TextBox TextBoxWorkOrderNumber = (TextBox)(DetailsView1.FindControl("TextBox11"));
            TextBox TextBoxRequestor = (TextBox)(DetailsView1.FindControl("TextBox3"));
            TextBox TextBoxDate = (TextBox)(DetailsView1.FindControl("TextBox1"));
            //TextBoxDate.Text = DateTime.Now.ToShortDateString();
            TextBox TextBoxDepartment = (TextBox)(DetailsView1.FindControl("TextBox4"));
            TextBox TextBoxCompletionDate = (TextBox)(DetailsView1.FindControl("TextBox16"));
            TextBox TextBoxMachineDescription = (TextBox)(DetailsView1.FindControl("TextBox5"));
            TextBox TextBoxMachineLocation = (TextBox)(DetailsView1.FindControl("TextBox6"));
            TextBox TextBoxWorkRequired = (TextBox)(DetailsView1.FindControl("TextBox7"));
            // DropDownList status = (DropDownList)(DetailsView1.FindControl("DropDownList2"));
            TextBox TextBoxStatus = (TextBox)(DetailsView1.FindControl("TextBox12"));
            TextBoxStatus.Text = "Open";
            DropDownList list = (DropDownList)(DetailsView1.FindControl("DropDownList1"));
            TextBox9.Text = list.SelectedValue;
            DropDownList lists = (DropDownList)(DetailsView1.FindControl("DropDownList2"));
            TextBox14.Text = lists.SelectedValue;

            if (TextBoxRequestor.Text.Length <= 0)
            {
                TextBoxRequestor.Text = "Not Applicable";
            }
            if (TextBox14.Text.Length <= 0)
            {
                TextBoxDepartment.Text = "Not Provided";
            }
            if (TextBoxCompletionDate.Text.Length <= 0)
            {
                TextBoxCompletionDate.Text = "Not Provided";
            }
            if (TextBoxMachineDescription.Text.Length <= 0)
            {
                TextBoxMachineDescription.Text = "Not Provided";
            }

            if (TextBoxMachineLocation.Text.Length <= 0)
            {
                TextBoxMachineLocation.Text = "Not Provided";
            }

            if (TextBoxWorkRequired.Text.Length <= 0)
            {
                TextBoxWorkRequired.Text = "Not Provided";
            }

        successfull = true;
    }
    else
        {
            e.Cancel = true;
          successfull = false;
        }


    }

这是实际插入的位置,它是我的sqldatasource:

** item_inserting中的所有值都插入此处,标识值为**

protected void RequestorSource_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (successfull == true)
            {
                try
                {
                    int newid = (int)e.Command.Parameters["@NewId"].Value;
                    TextBox13.Text = newid.ToString();
                }
                catch
                {
                    successfull = false;
                }
               if (e.AffectedRows == 1 && successfull == true)
               {
                   successfull = true;
               }
               else
               {
                   successfull = false;
               }
            }
            else
            {
                successfull = false;
            }


        }

问题是当我停止程序时电子邮件仍然被发送,我怎么能创建一个错误的插入,就像我希望它失败类似于用户发生的事情我无法重新创建问题,例如我试过在insert语句中添加一个新字段但没有给它任何值,并且它抛出的错误是你有更多的列然后是值。

这是我的详细信息视图:

enter image description here

** http://pastebin.com/6VC6FZK7处的所有.cs代码和http://pastebin.com/QhjWNNt0处的.aspx代码**希望这会有所帮助。

1 个答案:

答案 0 :(得分:0)

如果你只是在插入后将bool successful设置为true并且没有进行任何错误检查,那么它可能会失败并仍然发送电子邮件。我建议将insert代码包装在TRY..CATCH..中,并在succssful或运行catch中将IF...ELSE..状态设置为false,以检查数据是否为正确插入并在那里设置successful状态。