Hi there. I have this piece of code that should email the details some has entered and also add the details to a table in my sql databse. It seems to correct, the email sends fine, but nothing gets added to the table in the databse. Anybody able to see why?
using System;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class competition : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = txtFname.Text;
Label2.Text = txtLname.Text;
Label3.Text = txtEmail.Text;
Label4.Text = txtad1.Text;
Label5.Text = txtpostcode.Text;
Label6.Text = RadioButtonList1.SelectedValue;
Label7.Text = RadioButtonList2.SelectedValue;
Label8.Text = RadioButtonList3.SelectedValue;
Label9.Text = RadioButtonList4.SelectedValue;
Label10.Text = txtTieBreaker.Text;
}
protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
{
string strQuery;
SqlCommand cmd;
strQuery = "INSERT INTO competition (FirstName, LastName, PhoneNumber, Email, Address, PostCode, Question1, Question2, Question3, Question4, TieBreaker) values (@FirstName, @LastName, @PhoneNumber, @Email, @Address, @PostCode, @Question1, @Question2, @Question3, @Question4, @TieBreaker)";
cmd = new SqlCommand(strQuery);
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
StringBuilder sb = new StringBuilder();sb.Append("First Name: ");
sb.Append(txtFname.Text);
sb.Append("\r\n");
sb.Append("\r\n");sb.Append("Last Name: ");
sb.Append(txtLname.Text);
sb.Append("\r\n");
sb.Append("\r\n");sb.Append("Email: ");
sb.Append(txtEmail.Text);
sb.Append("\r\n");
sb.Append("\r\n");sb.Append("Phone Number: ");
sb.Append(txtMobile.Text);
sb.Append("\r\n");
sb.Append("\r\n");sb.Append("Address : ");
sb.Append(txtad1.Text);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Post Code: ");
sb.Append(txtpostcode.Text);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Question 1: ");
sb.Append(RadioButtonList1.SelectedItem.Value);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Question 2: ");
sb.Append(RadioButtonList2.SelectedItem.Value);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Question 3: ");
sb.Append(RadioButtonList3.SelectedItem.Value);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Question 4: ");
sb.Append(RadioButtonList4.SelectedItem.Value);
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Tie Breaker: ");
sb.Append(txtTieBreaker.Text);
sb.Append("\r\n");
sb.Append("\r\n");
MailAddress fromAddress = new MailAddress("email@mydomain.com");
smtpClient.Host = "localhost";
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "email@mydomain.com";
NetworkCred.Password = "password";smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = NetworkCred;
smtpClient.Port = 25;
message.From = fromAddress;
message.To.Add(txtEmail.Text);
message.Bcc.Add("email@mydomain.com");
message.Subject = "Entry";
message.IsBodyHtml = false;
message.Body = sb.ToString();
smtpClient.Send(message);
cmd.Parameters.AddWithValue("@FirstName", txtFname.Text);
cmd.Parameters.AddWithValue("@LastName", txtLname.Text);
cmd.Parameters.AddWithValue("@PhoneNumber", txtMobile.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Address", txtad1.Text);
cmd.Parameters.AddWithValue("@PostCode", txtpostcode.Text);
cmd.Parameters.AddWithValue("@Question1", RadioButtonList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Question2", RadioButtonList2.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Question3", RadioButtonList3.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Question4", RadioButtonList4.SelectedItem.Value);
cmd.Parameters.AddWithValue("@TieBreaker", txtTieBreaker.Text);
InsertUpdateData(cmd);
Response.Redirect("anotherpage.aspx");
}
private Boolean InsertUpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
}