views:

520

answers:

1

I am new to ASP.NET, and I am trying to create a contact form which sends an email at submission as well storing the data in a database.

I have read numerous tutorials, and I am pretty comfortable with a basic contact form to email setup - but I am having trouble with the database part of it.

Please let me know if you have ever done this, or if you could provide any information that would assist me with this task.

I am running ASP.NET 2.0

Thanks in advance.

+1  A: 

I'm very new to both C# and ASP.NET (first year IT student). Up until a month ago, I'd never even viewed C# code, much less programmed anything in C#. I, too, have been combing the internet for a solution to this problem. After a week of tinkering with some code, I finally figured it out. The below code will allow you to create an ASP.NET "Contact Us" email form, and will send the information in the form to a SQL Server database. I hope this helps someone to avoid the same aggravation I went through! (If anyone knows a way to program this more efficiently, I'd love to hear your thoughts.)

HERE IS THE CODE FOR THE ASPX.CS FILE ATTACHED TO THE FORM:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

//Code for the Reset event (will reset form data):

protected void Reset(object s, EventArgs e)
{
    fname.Text = "";
    lname.Text = "";
    email.Text = "";
    phone.Text = "";
    comments.Text = "";
}

//Code for the SendMail Event; will send email and write info in email into database:

protected void SendMail(object sender, EventArgs e)
{
    MailMessage mail = new MailMessage();
    mail.From = new MailAddress(email.Text);
    mail.To.Add("EMAIL ADDRESS WHERE YOU'D LIKE THE MESSAGE SENT");
    mail.Subject = "Contact Us";
    mail.IsBodyHtml = true;
    mail.Body += "First Name: " + fname.Text + "<br />";
    mail.Body += "Last Name: " + lname.Text + "<br />";
    mail.Body += "Comments: " + comments.Text + "<br />";
    mail.Body += "Phone Number: " + phone.Text + "<br />";

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "NAME OF SMTP RELAY SERVER";
    smtp.Send(mail);
}

protected void insertInfo(object sender, EventArgs e)
{
    SqlConnection myConnection = new SqlConnection             (ConfigurationManager.ConnectionStrings["WEB.CONFIG CONNECTION STRING NAME"].ToString());

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT INTO TABLE NAME (fname, lname, email, phone, comment)    
VALUES (@fname, @lname, @email, @phone, @comments)";
    cmd.Connection = myConnection;

    cmd.Parameters.Add("@fname", fname.Text);
    cmd.Parameters.Add("@lname", lname.Text);
    cmd.Parameters.Add("@email", email.Text);
    cmd.Parameters.Add("@phone", phone.Text);
    cmd.Parameters.Add("@comments", comments.Text);


    myConnection.Open();
    cmd.ExecuteNonQuery();
    myConnection.Close();
}

}
KP
Nice use of parameters for your SQL insert :) I would however recommend that you set `mail.IsBodyHtml` equal to `false` as you're not sanitizing the inputs there, and then use Environment.NewLine or "\n\r" to generate the breaks in the email. Taking it one step further, you might want to look at using a StringBuilder to build the body of the message, and use things like the AppendLine method to handle all of that for you (http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.appendline.aspx).
Zhaph - Ben Duguid
Thanks for the tip! It is much appreciated; I need all the advice I can get, being a newbie. I will follow your adivce.
KP
Thanks KP - your code has been a huge help!
megapixel