Sending an email and updating a Database

I have a formview to insert data into a DB.  Works great... I would also like to send an email after the user submits the form. 

 I put the mail message code on the .cs page and it worked great!!!!  Except it did not update the DB.......

I really need it to do both......... but one or the other works fine!!!

What am I missing here.....

 

As always thanks!!!!!

 

Show ur code.

You can do it easily with LINQ to sql. Example below,

            LinqDataContext dc = new LinqDataContext();
            LinqDC ct = new LinqDC();

            ct.Address = "Address";
            ct.Company = "Company";
            ct.Email = "Email";           

            dc.LinqDCs.InsertOnSubmit(ct);
            try
            {
                dc.SubmitChanges();
               
            }
            catch (Exception ex)
            {
                //handle exception
            }

This code works to update the DB..

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

using

 

System;

using

 

System.Collections.Generic;

using

 

System.Linq;

using

 

System.Web;

using

 

System.Web.UI;

using

 

System.Web.UI.WebControls;

using

 

System.Net.Mail;

public

 

partial class explorers_ridetime_riderequest : System.Web.UI.

Page

{

 

protected void Page_Load(object sender, EventArgs

e)

{

}

 

protected void date_requestedTextBox_TextChanged(object sender, EventArgs

e)

{

}

[System.Web.Services.

WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute

()]

 

public static int GetNextValue(int current, string

tag)

{

 

return default(int

);

}

 

protected void TextBox1_TextChanged(object sender, EventArgs

e)

{

}

 

protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs

e)

{

}

 

protected void InsertButton_Click(object sender, EventArgs

e)

{

 

 

}

[System.Web.Services.

WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute

()]

 

public static string GetDynamicContent(string

contextKey)

{

 

return default(string

);

}

}

 

This code works to send the email when added

 

 

 

 

 

 

 

 

 

MailMessage message = new MailMessage

();

{

message.From =

new MailAddress

(emailaddress.Text.ToString());

message.To.Add(

new MailAddress("xxxxx@xxxxxx.com"

));

message.CC.Add(

new MailAddress("xxxxxx@xxxxx.com"

));

message.Body = (

"join"

);

 

SmtpClient client = new SmtpClient

();

client.Host =

"mail.xxxxxx.com"

;

client.Send(message);

Response.Redirect(

http://www.xxxxxxxxxx.com/xxxxxx.htm

);

}

 

 THANKS!!!!!!!

Hi Jangle,

You should ensure that you invoke the function of sending the email.

You can put the code into page unload event and try it.

    protected void Page_Unload(object sender, EventArgs e) 
   
{ 
       //send the email

        }

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Thank you !!!!  And this works, except it works too good.

I would like the notification email send after the user inputs data and submits the form.  the way I have it, it seems to fire off the email each time the page is loaded.....

Any more help appreciated.....

Jim

This is the code behind....

 

public

 partial class explorers_ridetime_riderequest : System.Web.UI.

Page

{

 protected void Page_Load(object sender, EventArgs e)

{

}

 protected void date_requestedTextBox_TextChanged(object sender, EventArgs e)

{

}

 protected void TextBox1_TextChanged(object sender, EventArgs e)

{

}

 protected void AccessDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)

{

}

 protected void InsertButton_Click(object sender, EventArgs e)

{

}

 protected void Page_Unload(object sender, EventArgs e)

{

 MailMessage message = new MailMessage();

{

 

 

 

message.From = (

 

new MailAddress("XXX@XXXX.com"));

new MailAddress("XXX@XXXX.com"));

message.To.Add(

 

new MailAddress("XXX@XXXX.com"));

message.CC.Add(

 

"A new Ride Request has been posted");

message.Body = (

 

 SmtpClient client = new SmtpClient(); "mail.xxxxxx.com";

client.Host =

client.Send(message);

}

 }

}

thoughts on "Sending an email and updating a Database"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>