我正在努力解决一个我不知道如何解决的奇怪问题。我正在开发一个基于 Web 的小型事件管理系统。当用户单击将显示在 ModalPopUpExtender 控件中的注册按钮时,系统将执行以下操作:它将检查用户是否在数据库中。如果没有,它将从 Active Directory 中提取他的信息。然后,系统应检查用户是否已在此事件中注册。如果没有,他将在该事件中注册,并将向他发送一封确认电子邮件。如果是,将显示一条消息“您已经预订了此活动”。
我现在的问题是:系统会发送一封确认电子邮件,无论用户是否已预订。那么我应该怎么做才能修改我的代码,以便在他没有预订的唯一情况下向用户发送确认电子邮件?
C# 代码(对不起,代码冗长,但我把它放出来是为了澄清):
protected void btnSendConfirmationEmail_Click(object sender, EventArgs e)
{
checkUserID(userNetworkID);
SmtpClient sc = new SmtpClient("MailServer");
StringBuilder sb = new StringBuilder();
MailMessage msg = new MailMessage();
//Variables for retrieving the Booking Information
string title = lblTitle.Text;
string description = lblDescription.Text;
string location = lblLocation.Text;
string startDateTime = lblStartDateTime.Text;
string endDateTime = lblEndDateTime.Text;
//Message Information
string toAddress = userNetworkID + "@mailServer.com";
string fromAddress = "test@mailServer.com";
string mailSubject = "Registration Notification";
string messageBody = @".........................";
try
{
msg.To.Add(toAddress);
msg.From = new MailAddress(fromAddress, "Reg. Test System");
msg.Subject = mailSubject;
msg.Body = messageBody;
msg.IsBodyHtml = true;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
// something bad happened
//Response.Write("Something bad happened!");
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
}
protected void checkUserID(string userID)
{
int eventID = Convert.ToInt32(HiddenField1.Value);
string NetworkID = userID;
string Name = Service.GetName;
string BadgeNo = Service.GetBadgeNo;
string DepartmentCode = Service.GetDeptCode;
string connString = "Data Source=localhost;Initial Catalog=TestSysDB;Integrated Security=True;";
//if the user is not in the system database, add him
if (Security.isExisted(NetworkID) == false)
{
//string connString = "Data Source=localhost;Initial Catalog=TestSysDB;Integrated Security=True;";
string insertCommand = "INSERT INTO Users (NetworkID, Name, BadgeNo, DepartmentCode) values (@NetworkID, @Name, @BadgeNo, @DepartmentCode)";
using(SqlConnection conn = new SqlConnection(connString))
{
//Open DB Connection
conn.Open();
using(SqlCommand cmd = new SqlCommand(insertCommand, conn))
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@NetworkID", NetworkID);
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@BadgeNo", BadgeNo);
cmd.Parameters.AddWithValue("@DepartmentCode", DepartmentCode);
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
string insertBooking = "INSERT INTO BookingDetails (EventID, NetworkID) values (@EventID, @NetworkID)";
string selectCommand = "SELECT count(*) as UserBookings FROM BookingDetails WHERE NetworkID = NetworkID AND EventID = @EventID";
using (SqlConnection conn = new SqlConnection(connString))
{
//Open DB Connection
conn.Open();
using (SqlCommand cmd = new SqlCommand(selectCommand, conn))
{
cmd.Parameters.AddWithValue("@EventID", eventID);
cmd.Parameters.AddWithValue("@NetworkID", NetworkID);
if ((int)cmd.ExecuteScalar() == 0)
{
SqlCommand cmd2 = new SqlCommand(insertBooking, conn);
cmd2.Parameters.Clear();
cmd2.Parameters.AddWithValue("@EventID", eventID);
cmd2.Parameters.AddWithValue("@NetworkID", NetworkID);
cmd2.ExecuteNonQuery();
}
else
{
errorSpan.InnerText = "You already have a booking in this event";
}
}
//Close the connection
conn.Close();
}
}