重新加载页面时插入许多记录

时间:2020-02-01 12:07:35

标签: c# asp.net

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

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

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();
          }
      }
}

1 个答案:

答案 0 :(得分:0)

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

如果您仍要使用此设计,则必须记住是否已经承诺了某一行。您想知道该行具有“未保存的更改”。每行一个简单的布尔值可以做到这一点。使用WebApplications持久化数据是一个问题。在这种特定情况下-由于数据对安全性的影响很小-您可以将其发送到客户端,并将其作为Formular数据的一部分从客户端检索回来。

非常重要的规则:Web应用程序仍然像1980年的HTML Webformular一样,看上去不像桌面应用程序多少。所有旧的设计决策仍然适用。但幸运的是,旧的设计确实包含了hidden formular field。向用户发送和从用户发送回的东西,未显示。但是,如何使用ASP.Net中的此类字段有点超出我的知识。