我使用 c# 和 asp.net ,当用户创建新帐户时,将输入电子邮件和国家 ID 作为主键,然后如果用户单击 (VIRIFY ME) 按钮,程序将将此信息存储在 sql server 中,并向用户发送电子邮件消息,但我的问题是,当用户单击多个“更改我”按钮时,它会打印(电子邮件已存在)
protected void Button1_Click2(object sender, EventArgs e)
{
LabelErrorMSG.Text = "";
String email = emailtextbox0.Text.Trim();
String notionalID = textbox_National_ID.Text.Trim();
try
{
if (notionalID != "" && email != "" && counter==1)
{
// insert notional ID and email into database
getdataobj.PageSignUpInsert(notionalID, email);
/////////////////////////////////////////////////////////////////////
conn.Open();
//Generate Verification Code
String code = getdataobj.GetRandomNumber().ToString();
// Set Verification Code in database
SqlCommand comm = new SqlCommand("UPDATE trained SET VerificationCode='" + code + "' where NationalID='" + notionalID + "'", conn);
comm.ExecuteNonQuery();
conn.Close();
//Send Email to the user with Verification Code
SmtpClient smtpClient = new SmtpClient();
MailMessage mailMessage = new MailMessage("[email protected]", email, "", "");
mailMessage.To.Add(new MailAddress(email));
mailMessage.Subject = "Saudia Summer Traning";
mailMessage.Body = code;
smtpClient.EnableSsl = true;
smtpClient.Send(mailMessage);
Panel1.Visible = true;
}
else
{
LabelErrorMSG.Text = "you must insert national ID and email ";
}
////////////////////////////////////////////////////////////////////
}
catch (SqlException ex)
{
if (ex.Number == 2627)
{
if (ex.Message.Contains("UNIQUE"))
{
///error msg regarding Unique key violation.
LabelErrorMSG.Text = "The email already exist ";
}
if (ex.Message.Contains("PRIMARY"))
{
//error msg regarding Primary key violation.
LabelErrorMSG.Text = "The national ID already exist ";
}
}
}
}
当您希望用户可以多次单击验证按钮时,您只需插入一次 Emai 地址,因为您将其设置为唯一。在尝试插入之前,您可以检查电子邮件是否已插入。这应该可以防止这个错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句