somehow my code is not fully functioning.
These codes are fine:
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True");
SqlCommand cmd = new SqlCommand();
protected void btnSubmit_Click(object sender, EventArgs e)
{
if(Page.IsValid)
{
int userId = 0;
cmd.Connection = con; //assigning connection to command
cmd.CommandType = CommandType.Text; //representing type of command
cmd.CommandText = "INSERT INTO UserData values(@Username,@Firstname,@Lastname,@Email,@Password,@CustomerType,@DeliveryAddress,@Zip,@ContactNumber)";
//adding parameters with value
cmd.Parameters.AddWithValue("@Username", txtUser.Text);
cmd.Parameters.AddWithValue("@Firstname", txtFN.Text);
cmd.Parameters.AddWithValue("@Lastname", txtLN.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Password", (txtPW.Text));
cmd.Parameters.AddWithValue("@CustomerType", RadioButtonList1.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@DeliveryAddress", txtAddress.Text);
cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
cmd.Parameters.AddWithValue("@ContactNumber", txtContact.Text);
con.Open(); //opening connection
cmd.ExecuteNonQuery(); //executing query
SendActivationEmail(userId);
con.Close(); //closing connection
label_register_success.Text = "Registered Successfully..";
}
else
{
label_register_success.Text = "Please check the registration errors";
}
}
its registering to the sql database but it seems like its not reading the
SendActivation(userId)
up to the success registration message. Am i positioning this line of code wrong? by the way here is my sendactivation code:
private void SendActivationEmail(int userId)
{
string constr = ("Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True")
string activationCode = Guid.NewGuid().ToString();
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@UserId, @ActivationCode)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserId", userId);
cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
mm.Subject = "Account Activation";
string body = "Hello " + txtUser.Text.Trim() + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
Please help me out guys. thank you in advance.