【问题标题】:even when details view insert fails the application is sending an email即使详细信息视图插入失败,应用程序也会发送电子邮件
【发布时间】:2015-11-10 18:37:00
【问题描述】:

我有一个附加到 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 Inserting 我如何检查它是否正在插入到 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()

我只是成功地获得了一个布尔值;我在 Item_Inserting method() 的末尾设置为 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;
            }


        }

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

这是我的详细信息视图的图片:

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

【问题讨论】:

  • 请显示生成插入和填充successfull 变量的代码
  • 我尝试格式化您的代码,但有问题或遗漏。在if (TextBox9.Text == "Safety") 之后有两个{mm.Body 上的字符串"Work Required" 被连接到什么?
  • @MatthewVerstraete 我已经添加了你要求好友的代码
  • @AndersonPimentel 我已经修好了,请帮忙。
  • @MatthewVerstraete 是的,但我问的问题的重要部分是数据是否在通过 item_inserted 或 item_inserting 运行时插入到 sql 中,然后我可以将成功设置为 true。

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


【解决方案1】:

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

【讨论】:

  • 是的,我就是这么想的
猜你喜欢
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多