我掙扎着一個奇怪的問題,我不知道如何解決它。我正在開發一個小型的基於網絡的事件管理系統。當用戶點擊將顯示在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 = "[email protected]";
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();
}
}
BTW你的代碼是應該「活」在自己的圖層/類中的功能的混合體 - 提取SQL查詢(或使用NHibernate),ema il發送過程,電子郵件創建過程到分開的類。這將爲您節省後期頭痛... – 2012-08-14 07:23:22
刪除您的插入代碼,導致您只需檢查是否存在該用戶標識的事件 – JohnnBlade 2012-08-14 07:27:46