0

我正在努力解决一个我不知道如何解决的奇怪问题。我正在开发一个基于 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();
        }


    }
4

1 回答 1

1

像这样的东西

创建一个新方法UserHasBooking

private bool UserHasBooking(int userId, int eventID)
{
    bool result = false;

string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=RegistrationSysDB;Integrated Security=True;";
string selectCommand = "SELECT count(*) as UserBookingsCount 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", userId);
        if ((int)cmd.ExecuteScalar() > 0)
        {
            result = true;
        }
    }
    //Close the connection
    conn.Close();
}

    return result;
}


protected void btnSendConfirmationEmail_Click(object sender, EventArgs e)
    {
        int eventID = Convert.ToInt32(HiddenField1.Value);



        if(!UserHasBooking(userNetworkID, eventID))
        {

        checkUserID(userNetworkID);


        SmtpClient sc = new SmtpClient("MAIL.Aramco.com");
        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 + "@aramco.com";
        string fromAddress = "erms@aramco.com";
        string mailSubject = "Registration Notification";
        string messageBody = @"Greetings, <br /><br />
                               Your booking information is as following: <br /><br />
                               <b><u>Event Details</u></b> <br /><br />
                               <b>Title: </b>" + title +
                               "<br /> <b>Description: </b>" + description +
                               "<br /> <b>Location: </b>" + location +
                               "<br /> <b>Start Date & Time: </b>" + startDateTime +
                               "<br /> <b>End Date & Time: </b>" + endDateTime +
                               @"<br /><br /><br /><br /> 
                                 This email was generated using the <a href='http://pmv/PM_Registration_System/Default.aspx'>Events Registration Management System (ERMS) </a>. 
                                 Please do not reply to this email.";

        try
        {
            msg.To.Add(toAddress);
            msg.From = new MailAddress(fromAddress, "Events Registration Management 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();
            }

        }

       }

    }
于 2012-08-14T06:49:35.330 回答