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 に答える