locked
Store form data into database, then email same form data RRS feed

  • Question

  • User481677910 posted

    Hi,

    I am having issue with storing data into database and then email same form data. Most of the time it works fine but sometime it doesn't add data into database but do send email with all the data. 

    In button cs I have these setting

    First send email of the data

    then add into database

    Question is Above approach is fine or I have to first insert data into database and then email the data?

    Monday, September 9, 2019 11:16 AM

Answers

  • User-719153870 posted

    Hi new2world2015,

     where should I place the email code in the coding. After cmd.Connection
    = con;   ?

    Sorry for not replying in time and 'no' for the question you asked, cmd.Connection = con; is just setting connection for cmd.

    cmd.ExecuteNonQuery(); is where cmd doing the operation you setted before in strQuery.

    So, if you want to do the mail-sending after you stored related data, the code should be modified like below:

    protected void Upload_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile || FileUpload1.HasFile && FileUpload2.HasFile)
                {
                    string FileName2 = Path.GetFileName(FileUpload2.PostedFile.FileName);
                    string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
    
                    //Save files to disk
    
                    //     FileUpload1.SaveAs(Server.MapPath("../images1/") + FileName1);
    
                    //Add Entry to DataBase
    
                    Guid newGUID = Guid.NewGuid();
    
                    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["cyString"].ConnectionString;
    
                    Session["UserName"] = HttpContext.Current.User.Identity.Name;
    
                    SqlConnection con = new SqlConnection(strConnString);
    
                    string strQuery = "insert into DB_110006_customer_Order (Guid, Id, username,  Order_Name)" + " values ( @Guid, @Id, @Username  @Order_Name)";
    
                    SqlCommand cmd = new SqlCommand(strQuery);
    
                    cmd.Parameters.AddWithValue("@Guid", newGUID.ToString());
                    cmd.Parameters.AddWithValue("@IP", Request.UserHostAddress);
                    cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey);
                    cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
                    cmd.Parameters.AddWithValue("@Order_Name", OrderName.Text);
    
                    cmd.CommandType = CommandType.Text;
    
                    cmd.Connection = con;
    
                    try
                    {
                        con.Open();
                        //you want to put your email-sending after below
                        if (cmd.ExecuteNonQuery() > 0)//cmd.ExecuteNonQuery() will return the number of affected rows, if >0 that means the insert operation is successfully executed
                        {
                            //this is where you send your email
                            int DG1;
                            Random rand1 = new Random();
                            DG1 = rand1.Next(0, 100);
    
                            MailMessage mail1 = new MailMessage();
                            MailAddress fromMail1 = new MailAddress("test@test.com");
    
                            mail1.From = fromMail1;
                            mail1.To.Add(new MailAddress("test1@gmail.com"));
                            mail1.Bcc.Add(new MailAddress("test2@gmail.com"));
                            mail1.Subject = "Thank you for placing the order" + DG1 + "(" + OrderName.Text + ")";
                            var body1 = " Thank you for placing the order <br /><br />";
                            body1 += " Thank you for placing the order for <br /><br />";
    
                            mail1.Body = body1;
                            mail1.IsBodyHtml = true;
                            //Attach file using FileUpload Control and put the file in memory stream
                            if (FileUpload1.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                            }
    
                            if (FileUpload2.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
                            }
    
                            var smtp1 = new System.Net.Mail.SmtpClient();
                            {
                                smtp1.Host = "mail.test.com";
                                smtp1.Port = 25;
                                smtp1.EnableSsl = false;
                                smtp1.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
                                smtp1.Credentials = new NetworkCredential("test@test.com", "password");
                                smtp1.Timeout = 20000;
                            }
                            smtp1.Send(mail1);
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        Response.Redirect("2ndpage.aspx");
                    }
                }
            }

    Hope this would help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2019 2:09 AM

All replies

  • User753101303 posted

    Hi,

    Yes it would be better to send the mail once you are sure data are persisted on the db side.

    Not sure how you handle errors duruing the insert. Only the user knows something wrong happened? By default exceptions are written to the Windows event log and you should perhaps check that to find and possibly fix your insert issue.

    Monday, September 9, 2019 11:32 AM
  • User-719153870 posted

    Hi new2world2015,

    I am having issue with storing data into database and then email same form data.

    In button cs I have these setting

    First send email of the data

    then add into database

    This is little confusing here, but first thing first, sending email after you stored the data is a better choice usually.

    Most of the time it works fine but sometime it doesn't add data into database but do send email with all the data. 

    I think there might be some problems in your code that cause the data inserting failed.

    If possible, please share related code to see if something is wrong.

    Best Regard,

    Yang Shen

    Tuesday, September 10, 2019 6:24 AM
  • User481677910 posted

    Yang Shen

    Hi new2world2015,

    new2world2015

    I am having issue with storing data into database and then email same form data.

    new2world2015

    In button cs I have these setting

    First send email of the data

    then add into database

    This is little confusing here, but first thing first, sending email after you stored the data is a better choice usually.

    new2world2015

    Most of the time it works fine but sometime it doesn't add data into database but do send email with all the data. 

    I think there might be some problems in your code that cause the data inserting failed.

    If possible, please share related code to see if something is wrong.

    Best Regard,

    Yang Shen

    Code is below

      protected void Upload_Click(object sender, EventArgs e)
        {
    
            {
                int DG1;
                Random rand1 = new Random();
                DG1 = rand1.Next(0, 100);
    
                MailMessage mail1 = new MailMessage();
                MailAddress fromMail1 = new MailAddress("test@test.com");
    
                mail1.From = fromMail1;
                mail1.To.Add(new MailAddress("test1@gmail.com"));
                mail1.Bcc.Add(new MailAddress("test2@gmail.com"));
                mail1.Subject = "Thank you for placing the order" + DG1 + "(" + OrderName.Text + ")";
                var body1 = " Thank you for placing the order <br /><br />";
                body1 += " Thank you for placing the order for <br /><br />";
                
    
              
                mail1.Body = body1;
                mail1.IsBodyHtml = true;
                //Attach file using FileUpload Control and put the file in memory stream
                if (FileUpload1.HasFile)
                {
                    mail1.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                }
    
                if (FileUpload2.HasFile)
                {
                    mail1.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
                }
    
                var smtp1 = new System.Net.Mail.SmtpClient();
                {
                    smtp1.Host = "mail.test.com";
                    smtp1.Port = 25;
                    smtp1.EnableSsl = false;
                    smtp1.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
                    smtp1.Credentials = new NetworkCredential("test@test.com", "password");
                    smtp1.Timeout = 20000;
                }
                smtp1.Send(mail1);
            }
    
    
           
    
    
    
            if (FileUpload1.HasFile || FileUpload1.HasFile && FileUpload2.HasFile)
            {
    
                string FileName2 = Path.GetFileName(FileUpload2.PostedFile.FileName);
                string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
    
    
                //Save files to disk
    
    
                //     FileUpload1.SaveAs(Server.MapPath("../images1/") + FileName1);
    
                //Add Entry to DataBase
    
                Guid newGUID = Guid.NewGuid();
    
                String strConnString = System.Configuration.ConfigurationManager
    
                    .ConnectionStrings["cyString"].ConnectionString;
    
                Session["UserName"] = HttpContext.Current.User.Identity.Name;
    
                SqlConnection con = new SqlConnection(strConnString);
    
                string strQuery = "insert into DB_110006_customer_Order (Guid, Id, username,  Order_Name)" +
    
                    " values ( @Guid, @Id, @Username  @Order_Name)";
    
                SqlCommand cmd = new SqlCommand(strQuery);
    
                cmd.Parameters.AddWithValue("@Guid", newGUID.ToString());
                cmd.Parameters.AddWithValue("@IP", Request.UserHostAddress);
                cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey);
                cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
                cmd.Parameters.AddWithValue("@Order_Name", OrderName.Text);
                
               
                cmd.CommandType = CommandType.Text;
    
                cmd.Connection = con;
    
    
    
    
                try
                {
    
                    con.Open();
    
                    cmd.ExecuteNonQuery();
    
                }
    
                catch (Exception ex)
                {
    
                    Response.Write(ex.Message);
    
                }
    
                finally
                {
    
                    con.Close();
    
    
                    con.Dispose();
                    Response.Redirect("2ndpage.aspx");
                }
    
            }
    
    
    
        }
    }

    Please do let me know if email after insert would be better where should I place the email code in the coding. After cmd.Connection = con;   ?

    Tuesday, September 10, 2019 8:32 PM
  • User-719153870 posted

    Hi new2world2015,

     where should I place the email code in the coding. After cmd.Connection
    = con;   ?

    Sorry for not replying in time and 'no' for the question you asked, cmd.Connection = con; is just setting connection for cmd.

    cmd.ExecuteNonQuery(); is where cmd doing the operation you setted before in strQuery.

    So, if you want to do the mail-sending after you stored related data, the code should be modified like below:

    protected void Upload_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile || FileUpload1.HasFile && FileUpload2.HasFile)
                {
                    string FileName2 = Path.GetFileName(FileUpload2.PostedFile.FileName);
                    string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
    
                    //Save files to disk
    
                    //     FileUpload1.SaveAs(Server.MapPath("../images1/") + FileName1);
    
                    //Add Entry to DataBase
    
                    Guid newGUID = Guid.NewGuid();
    
                    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["cyString"].ConnectionString;
    
                    Session["UserName"] = HttpContext.Current.User.Identity.Name;
    
                    SqlConnection con = new SqlConnection(strConnString);
    
                    string strQuery = "insert into DB_110006_customer_Order (Guid, Id, username,  Order_Name)" + " values ( @Guid, @Id, @Username  @Order_Name)";
    
                    SqlCommand cmd = new SqlCommand(strQuery);
    
                    cmd.Parameters.AddWithValue("@Guid", newGUID.ToString());
                    cmd.Parameters.AddWithValue("@IP", Request.UserHostAddress);
                    cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey);
                    cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
                    cmd.Parameters.AddWithValue("@Order_Name", OrderName.Text);
    
                    cmd.CommandType = CommandType.Text;
    
                    cmd.Connection = con;
    
                    try
                    {
                        con.Open();
                        //you want to put your email-sending after below
                        if (cmd.ExecuteNonQuery() > 0)//cmd.ExecuteNonQuery() will return the number of affected rows, if >0 that means the insert operation is successfully executed
                        {
                            //this is where you send your email
                            int DG1;
                            Random rand1 = new Random();
                            DG1 = rand1.Next(0, 100);
    
                            MailMessage mail1 = new MailMessage();
                            MailAddress fromMail1 = new MailAddress("test@test.com");
    
                            mail1.From = fromMail1;
                            mail1.To.Add(new MailAddress("test1@gmail.com"));
                            mail1.Bcc.Add(new MailAddress("test2@gmail.com"));
                            mail1.Subject = "Thank you for placing the order" + DG1 + "(" + OrderName.Text + ")";
                            var body1 = " Thank you for placing the order <br /><br />";
                            body1 += " Thank you for placing the order for <br /><br />";
    
                            mail1.Body = body1;
                            mail1.IsBodyHtml = true;
                            //Attach file using FileUpload Control and put the file in memory stream
                            if (FileUpload1.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                            }
    
                            if (FileUpload2.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
                            }
    
                            var smtp1 = new System.Net.Mail.SmtpClient();
                            {
                                smtp1.Host = "mail.test.com";
                                smtp1.Port = 25;
                                smtp1.EnableSsl = false;
                                smtp1.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
                                smtp1.Credentials = new NetworkCredential("test@test.com", "password");
                                smtp1.Timeout = 20000;
                            }
                            smtp1.Send(mail1);
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        Response.Redirect("2ndpage.aspx");
                    }
                }
            }

    Hope this would help.

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 12, 2019 2:09 AM
  • User481677910 posted

    Yang Shen

    Hi new2world2015,

    new2world2015

    where should I place the email code in the coding. After cmd.Connection
    = con;   ?

    Sorry for not replying in time and 'no' for the question you asked, cmd.Connection = con; is just setting connection for cmd.

    cmd.ExecuteNonQuery(); is where cmd doing the operation you setted before in strQuery.

    So, if you want to do the mail-sending after you stored related data, the code should be modified like below:

    protected void Upload_Click(object sender, EventArgs e)
            {
                if (FileUpload1.HasFile || FileUpload1.HasFile && FileUpload2.HasFile)
                {
                    string FileName2 = Path.GetFileName(FileUpload2.PostedFile.FileName);
                    string FileName1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
    
                    //Save files to disk
    
                    //     FileUpload1.SaveAs(Server.MapPath("../images1/") + FileName1);
    
                    //Add Entry to DataBase
    
                    Guid newGUID = Guid.NewGuid();
    
                    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["cyString"].ConnectionString;
    
                    Session["UserName"] = HttpContext.Current.User.Identity.Name;
    
                    SqlConnection con = new SqlConnection(strConnString);
    
                    string strQuery = "insert into DB_110006_customer_Order (Guid, Id, username,  Order_Name)" + " values ( @Guid, @Id, @Username  @Order_Name)";
    
                    SqlCommand cmd = new SqlCommand(strQuery);
    
                    cmd.Parameters.AddWithValue("@Guid", newGUID.ToString());
                    cmd.Parameters.AddWithValue("@IP", Request.UserHostAddress);
                    cmd.Parameters.AddWithValue("@Id", Membership.GetUser().ProviderUserKey);
                    cmd.Parameters.AddWithValue("@Username", Session["UserName"]);
                    cmd.Parameters.AddWithValue("@Order_Name", OrderName.Text);
    
                    cmd.CommandType = CommandType.Text;
    
                    cmd.Connection = con;
    
                    try
                    {
                        con.Open();
                        //you want to put your email-sending after below
                        if (cmd.ExecuteNonQuery() > 0)//cmd.ExecuteNonQuery() will return the number of affected rows, if >0 that means the insert operation is successfully executed
                        {
                            //this is where you send your email
                            int DG1;
                            Random rand1 = new Random();
                            DG1 = rand1.Next(0, 100);
    
                            MailMessage mail1 = new MailMessage();
                            MailAddress fromMail1 = new MailAddress("test@test.com");
    
                            mail1.From = fromMail1;
                            mail1.To.Add(new MailAddress("test1@gmail.com"));
                            mail1.Bcc.Add(new MailAddress("test2@gmail.com"));
                            mail1.Subject = "Thank you for placing the order" + DG1 + "(" + OrderName.Text + ")";
                            var body1 = " Thank you for placing the order <br /><br />";
                            body1 += " Thank you for placing the order for <br /><br />";
    
                            mail1.Body = body1;
                            mail1.IsBodyHtml = true;
                            //Attach file using FileUpload Control and put the file in memory stream
                            if (FileUpload1.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload1.PostedFile.InputStream, FileUpload1.FileName));
                            }
    
                            if (FileUpload2.HasFile)
                            {
                                mail1.Attachments.Add(new Attachment(FileUpload2.PostedFile.InputStream, FileUpload2.FileName));
                            }
    
                            var smtp1 = new System.Net.Mail.SmtpClient();
                            {
                                smtp1.Host = "mail.test.com";
                                smtp1.Port = 25;
                                smtp1.EnableSsl = false;
                                smtp1.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
                                smtp1.Credentials = new NetworkCredential("test@test.com", "password");
                                smtp1.Timeout = 20000;
                            }
                            smtp1.Send(mail1);
                        }
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    finally
                    {
                        con.Close();
                        con.Dispose();
                        Response.Redirect("2ndpage.aspx");
                    }
                }
            }

    Hope this would help.

    Best Regard,

    Yang Shen

    Its emailing for now until we get any error from our client.  Although I have to remove this code from the above as giving me error and place like below is what working.

     protected void Upload_Click(object sender, EventArgs e)
        {
    
            int DG;
            Random rand = new Random();
            DG = rand.Next(0, 100);

    above working with your coding and if we place after try { it's not working.

    Wednesday, September 25, 2019 3:25 PM