我有一个表格。我正在将数据插入我的数据库并将这些插入的详细信息发送到邮件。
单击提交按钮后,我正在清除所有变量,但我的问题是当我再次重新加载页面数据插入作为新记录并且正在发送其他邮件时。我不想插入新记录并发送另一封邮件。
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();
}
}
}
通常,像这样进行批量更改或提交不是一个好的设计。您希望用户仅添加、编辑或删除一个特定条目。并在用户完成该操作的那一刻提交。收集这样的更改通常不是一个好主意。它增加了数据丢失的危险,并以指数方式更新争用条件。它几乎不适用于IMAP,IIRC的设计更像是一个适当的,多插入的分布式数据库。
如果您仍想使用此设计,则必须记住是否已提交一个特定行。您想知道哪一行有"未保存的更改"。每行的简单布尔值就可以做到这一点。使用Web应用程序持久化数据是一个问题。在此特定情况下,由于数据的安全影响非常低,因此您可以将其发送到客户端,并将其作为公式器数据的一部分从客户端检索。
非常重要的规则:不管它们看起来有多像桌面应用程序,Web应用程序仍然是1980年的HTML Webformular。所有旧的设计决策仍然适用。但幸运的是,旧设计确实包含一个隐藏的公式字段。不显示向用户发送和从用户发回的内容。然而,如何使用 ASP.Net 这样的字段有点超出我的知识范围。