【发布时间】: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”布尔值。将其存储在隐藏的公式字段中。插入数据库成功后设置。虽然这不是出于安全目的而保存的,但对于这种情况(意外双发)来说已经足够可靠了。
-
对不起,我没有得到你。能不能详细解释一下。