【问题标题】:Inserting many records while reloading the page重新加载页面时插入许多记录
【发布时间】:2020-02-01 12:07:35
【问题描述】:

我有一个表格。我正在将数据插入我的数据库并将这些插入的详细信息发送到邮件。

单击提交按钮后,我正在清除所有变量,但我的问题是当我再次重新加载页面时,数据作为新记录插入并且正在发送另一封邮件。我不想插入新记录并发送另一封邮件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;

namespace SupportPortal
{
    public partial class Support : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void clearallfields()
        {
            ProblemName.Value = "";
            ImpactDropDown.SelectedIndex = 0;
            SeverityDropDown.SelectedIndex = 0;
            ProblemDescription.Value = "";
        }

        protected void Submitsupportform_Click(object sender, EventArgs e)
        {
            //string ticketNumber = string.Empty;
            string Problem = ProblemName.Value;
            string impact = ImpactDropDown.Value;
            string Priority = SeverityDropDown.Value;
            string problemdescription = ProblemDescription.Value;
            Byte[] bytImage = new byte[] { 1 };
            Byte[] bytImage1 = new byte[] { 1 };
            Byte[] bytImage2 = new byte[] { 1 };
            string FileName = "";

            try
            {  
                // Get the HttpFileCollection
                HttpFileCollection hfc = Request.Files;

                for (int i = 0; i < hfc.Count; i++)
                {
                    HttpPostedFile hpf = hfc[i];
                    FileName = System.IO.Path.GetFullPath(hpf.FileName);
                    HttpPostedFile objHttpPostedFile = Request.Files[Request.Files.AllKeys[i]];
                    int intContentlength = objHttpPostedFile.ContentLength;

                    if (i == 0)
                    {
                        bytImage = new Byte[intContentlength];
                        objHttpPostedFile.InputStream.Read(bytImage, 0, intContentlength);
                    }

                    if (i == 1)
                    {
                        bytImage = new Byte[intContentlength];
                        objHttpPostedFile.InputStream.Read(bytImage1, 0, intContentlength);
                    }

                    if (i == 2)
                    {
                        bytImage = new Byte[intContentlength];
                        objHttpPostedFile.InputStream.Read(bytImage2, 0, intContentlength);
                    }
                }
            }
            catch (Exception ex) 
            {
                throw ex; 
            }

            // inserting into database
            SqlConnection con = new SqlConnection("Server=localhost;Database=ViveSupport;Integrated Security=SSPI");

            SqlCommand cmd = new SqlCommand("CreateTicket", con);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.AddWithValue("@Product", ProductName.Value);
            cmd.Parameters.AddWithValue("@Version", ProductVersionDropDown.Value);
            cmd.Parameters.AddWithValue("@Module", ModuleDropDown.Value);
            cmd.Parameters.AddWithValue("@OperatingSystem", OSDropDown.Value);
            cmd.Parameters.AddWithValue("@DataSource", Datasource.Value);
            cmd.Parameters.AddWithValue("@Browser", BrowserDropDown.Value);
            cmd.Parameters.AddWithValue("@Attachment1", bytImage);
            cmd.Parameters.AddWithValue("@Attachment2", bytImage1);
            cmd.Parameters.AddWithValue("@Attachment3", bytImage2);

            con.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);

            String ticketNumber = ds.Tables[0].Rows[0]["ticketNumber"].ToString();
            con.Close();

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[6] { 
                                    new DataColumn("Product", typeof(string)),
                                    new DataColumn("Module",typeof(string)),
                                    new DataColumn("Product version", typeof(string)),
                                    new DataColumn("OS",typeof(string)), 
                                    new DataColumn("Datasource", typeof(string)),
                                    new DataColumn("Browser",typeof(string))});
            dt.Rows.Add(ProductName, ModuleDropDown, ProductVersionDropDown, OSDropDown, Datasource, BrowserDropDown);

            StringBuilder sb = new StringBuilder();

            // Table start
            sb.Append("<table cellpadding='5' cellspacing='0' style='border: 1px solid #ccc;font-size: 9pt;font-family:Arial'>");

            // Adding HeaderRow
            sb.Append("<tr>");

            foreach (DataColumn column in dt.Columns)
            {
                sb.Append("<th style='background-color: #f5f5f5;border: 1px solid #ccc;text-align: left;'>" + column.ColumnName + "</th>");
            }

            sb.Append("</tr>");

            // Adding DataRow
            foreach (DataRow row in dt.Rows)
            {
                sb.Append("<tr>");

                foreach (DataColumn column in dt.Columns)
                {
                    sb.Append("<td style='width:100px;border: 1px solid #ccc'>" + row[column.ColumnName].ToString() + "</td>");
                }

                sb.Append("</tr>");
            }

            // Table end
            sb.Append("</table>");

            StringBuilder problemtable = new StringBuilder();
            problemtable.Append("<div><div><table style='font-size: 9pt;font-family:Arial'><tr><td>Problem: </td><td>" + Problem + "</td></tr><tr><td>Impact: </td><td>" + impact + "</td></tr><tr><td>Priority: </td><td>" + Priority + "</td></tr><tr><td>ProblemDescription: </td><td>" + problemdescription + "</td></tr></table></div></div>");

            StringBuilder footersignature = new StringBuilder();
            string to = ""; //To address    
            string from = ""; //From address    

            MailMessage message = new MailMessage(from, to);
            string mailbody = sb.ToString() + problemtable.ToString();
            message.Subject = "Generated ticket Number is" + ticketNumber;
            message.Body = mailbody;
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;

            for (var x = 0; x < Request.Files.AllKeys.Length; x++)
            {
                string file = System.IO.Path.GetFullPath(upload_file1.PostedFile.FileName);
                // HttpPostedFile file = Request.Files[Request.Files.AllKeys[x]];

                if (file != null && file.Length > 0)
                { 
                     try
                     {
                         message.Attachments.Add(new Attachment(Path.GetFileName(System.IO.Path.GetFileName(file))));  
                     }
                     catch (Exception ex)
                     {
                         throw ex;
                     }
                 }
              }

              SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp    
              System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential("", "");

              client.EnableSsl = true;
              client.UseDefaultCredentials = false;
              client.Credentials = basicCredential1;

              try
              {
                  client.Send(message);
              }
              catch (Exception ex)
              {   
                  throw ex;
              }

              // method to clear all the fields
              clearallfields();
          }
      }
}

【问题讨论】:

  • 添加一个“Comitted”布尔值。将其存储在隐藏的公式字段中。插入数据库成功后设置。虽然这不是出于安全目的而保存的,但对于这种情况(意外双发)来说已经足够可靠了。
  • 对不起,我没有得到你。能不能详细解释一下。

标签: c# asp.net


【解决方案1】:

通常像这样进行批量更改或提交并不是一个好的设计。您希望用户只添加、编辑或删除一个特定条目。并在用户完成该操作的那一刻提交。像这样收集更改通常不是一个好主意。它增加了数据丢失的危险,并以指数方式更新竞争条件。它对 IMAP 和 IIRC 几乎不起作用,它的设计更像是一个适当的、多插入的分布式数据库。

如果你想继续使用这个设计,你必须记住是否已经有一个特定的行。您想知道哪一行有“未保存的更改”。每行的一个简单的布尔值就可以做到这一点。使用 WebApplications 持久化数据是一个问题。在这种特定情况下 - 由于数据的安全影响非常低 - 您可以将其发送给客户端,然后将其作为公式数据的一部分从客户端取回。

非常重要的规则:无论它们看起来多么像桌面应用程序,Web 应用程序仍然是 1980 年的 HTML Webformular。所有旧的设计决策仍然适用。但幸运的是,旧设计确实包含hidden formular field。发送给用户和从用户发回的东西,没有显示出来。但是,如何使用 ASP.Net 中的此类字段有点超出我的知识范围。

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 2020-03-11
    • 2012-08-17
    • 2011-07-21
    • 2011-04-13
    • 2020-05-20
    • 1970-01-01
    • 2021-12-23
    • 2015-03-05
    相关资源
    最近更新 更多