I am struggling with a little strange problem that I don’t know how to solve it. I am developing a small web-based events management system. When the user clicks on the Register Button that will be shown in a ModalPopUpExtender control, the system will do the following:
it will check if the user is in the database or not. if not, it will pull his information from the Active Directory.
Then, the system should check if the user has been registered in this event or not. if not, he will be registered in that event and a confirmation email will be sent to him. If yes, a message will be displayed saying “You already have a booking in this event.”
My problem now is: the system sends a confirmation email whether the user has booking or not in that event. So what I should do to modify my code in order to send a confirmation email to the user in the only case that he has no booking in that event?
C# Code (sorry for the lengthy code, but I put it for clarification):
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();
}
}
Something like this
create a new method
UserHasBooking