Click here to Skip to main content
15,885,213 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send email retrieving the current row when the user click the "YES" option. However, the email won't populate the current record - populating the previous record instead. Please help.'


SqlConnection con3 = new SqlConnection(ConfigurationManager.ConnectionStrings
["Personnel_Info_ConnectionString"]"ConnectionString);


protected void btnInsert_Data_Click(object sender, EventArgs e)
{        
    SqlCommand cmd1 = new SqlCommand
    (
            "INSERT INTO Users.[Employee] VALUES " + 
            " (@txtFirstName, @txtLastName, @txtTitle, @txtEmail, @txtPhone, @txtEffective_Date, " +
            " @Email_Access_Boolean, @Remote_Access_Boolean, @Employee_Access_Boolean, " +
            " @Parking_Access_Boolean, @Security_Risk_Boolean, @Authorize_Purchase_Boolean, " + 
            " @Point_Contact_ID, @txtManager, @Dept_ID)", 
             con
        );

        System.Threading.Thread.Sleep(3000);

        cmd1.Parameters.AddWithValue("@txtFirstName", txtFirstName.Text);
        cmd1.Parameters.AddWithValue("@txtLastName", txtLastName.Text);
        cmd1.Parameters.AddWithValue("@txtTitle", txtTitle.Text);
        cmd1.Parameters.AddWithValue("@txtEmail", this.txtEmail.Text.Trim());
        cmd1.Parameters.AddWithValue("@txtPhone", txtPhone.Text);
        cmd1.Parameters.AddWithValue("@txtEffective_Date", txtEffective_Date.Text);
 


        /*** Email Access logic ***/
        string Email_Access = string.Empty;
        // cmd1.Parameters.AddWithValue("@Email_Access_Boolean", Email_Access);
        
        if (Email_Access_YES.Checked)
        {
            Email_Access = "Yes";
            // cmd1.Parameters.AddWithValue("@Email_Access_Boolean", Email_Access);

           
            /*** Send email to IS ***/
            MailMessage mail_IS = new MailMessage("TestingApp5677@gmail.com", "jyuen@stbh.org");
            mail_IS.IsBodyHtml = true;
            SmtpClient client_IS = new SmtpClient("smtp.gmail.com", 587);

            mail_IS.Body = "<table>";
            StringBuilder Email_Access_builder = new StringBuilder();

            Email_Access_builder.Append("Please process the following employee: ");
            Email_Access_builder.Append("<br/> <br/>");
            Email_Access_builder.Append
            (
                "<table border='4px' cellpadding='3' cellspacing='4' bgcolor='lightyellow' style='font-family:Garamond; font-size:medium'>"
            );
            Email_Access_builder.Append("<tr>");


            /*** Database content sent to email  ***/
            String Email_Access_SQL =
                "SELECT FirstName AS [First Name], LastName AS [Last Name], Dept AS [Department], Manager FROM Users.Employee (nolock) "
                + "WHERE ID = (SELECT MAX(ID) FROM Users.Employee WHERE Email_Access = 'Yes')";

            SqlDataAdapter Email_Access_adapter = new SqlDataAdapter(Email_Access_SQL, con3);
            DataSet Email_Access_data = new DataSet();
            Email_Access_adapter.Fill(Email_Access_data);
            

            foreach (DataColumn Column_Header in Email_Access_data.Tables[0].Columns)
            {
                Email_Access_builder.Append("<td >");
                Email_Access_builder.Append(Column_Header.ColumnName);
                Email_Access_builder.Append("</td>");
            }
            Email_Access_builder.Append("</tr>");


            foreach (DataRow Row_Info in Email_Access_data.Tables[0].Rows)
            {
                Email_Access_builder.Append("<tr>");

                foreach (DataColumn Column_Info in Email_Access_data.Tables[0].Columns)
                {
                    Email_Access_builder.Append("<td >");
                    Email_Access_builder.Append(Row_Info[Column_Info.ColumnName].ToString());
                    Email_Access_builder.Append("</td>");
                }
            }
            Email_Access_builder.Append("</tr>");
            Email_Access_builder.Append("</table>");
            Email_Access_builder.Append("<br/>"); 
            Email_Access_builder.Append("Thanks for your help!");
            
            mail_IS.Body = Email_Access_builder.ToString();

            client_IS.DeliveryMethod = SmtpDeliveryMethod.Network;
            client_IS.UseDefaultCredentials = false;
            client_IS.Host = "smtp.gmail.com";
            mail_IS.CC.Add(new MailAddress("ahosu@stbh.org"));
            mail_IS.Subject = "Personnel Confirmation for Email Access";
            

            client_IS.EnableSsl = true;
            client_IS.Credentials = new System.Net.NetworkCredential()
            {
                UserName = "*****@gmail.com",
                Password = "****"
            };

            client_IS.Send(mail_IS);
            // cmd1.Parameters.AddWithValue("@Email_Access_Boolean", Email_Access);
        }
        else
        {
            Email_Access = "No";
        }
        cmd1.Parameters.AddWithValue("@Email_Access_Boolean", Email_Access);


What I have tried:

Please advise why the sent email populates the previous record from the "Selected" option value.
Posted
Updated 22-Jun-18 6:36am

1 solution

You're retrieving the "last" record added (MAX(ID)) "before" you do the "insert" of your new record.

(I actually don't see any "insert", but I assume it's somewhere at the "bottom" based on your parameter assignments).
 
Share this answer
 
Comments
Lay_Kay 22-Jun-18 12:43pm    
Isn't MAX(ID) getting the latest record?


SqlCommand cmd1 = new SqlCommand
(
     "INSERT INTO Users.[Employee] VALUES " + 
     " (@txtFirstName, @txtLastName, @txtTitle, @txtEmail, @txtPhone, 
 @txtEffective_Date, " +
            " @Email_Access_Boolean, @Remote_Access_Boolean, @Employee_Access_Boolean, " +
            " @Parking_Access_Boolean, @Security_Risk_Boolean, @Authorize_Purchase_Boolean, " + 
            " @Point_Contact_ID, @txtManager, @Dept_ID)", 
             con
);


I thought this is already inserting records. Please advise. Thanks!
[no name] 22-Jun-18 13:02pm    
That shows the "command"; it doesn't "execute" it.
Lay_Kay 22-Jun-18 13:13pm    
Where (in the script) can I execute the INSERT command? Please advise.
Lay_Kay 22-Jun-18 12:46pm    
I also have the following coding:

 con.Open();
 cmd1.ExecuteNonQuery();

 Personel_Grid.DataBind();
 con.Close();

 lbl_Email.Visible = true;
 lbl_Email.Text = "The Personnel Process started.";


 txtFirstName.Text = "";
 txtLastName.Text = "";
 txtTitle.Text = "";
 txtManager.Text = "";
 txtEmail.Text = "";
 txtPhone.Text = "";
 txtEffective_Date.Text = "";
 lbl_Success.Text = "";
}

[no name] 22-Jun-18 13:06pm    
NOW you show the "execute" ... but it's not clear "when" you are doing it.

(Not in the right place based on your original post).

And what happens if SOMEONE ELSE "adds a NEW record" before you do your "MAX(ID)"? Whose record do you think you'll get?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900