protected void Page_Load(object sender, EventArgs e) {
} protected void submit_Click(object sender, EventArgs e) { SqlDataSource sds = new SqlDataSource(); sds.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString(); sds.SelectParameters.Add("username", TypeCode.String, this.username.Text); sds.SelectCommand = "SELECT * FROM [users] WHERE [username] = @username"; DataView dv = (DataView)sds.Select(DataSourceSelectArguments.Empty); try { if (dv.Count == 0) { SqlDataSource emailcheck = new SqlDataSource(); emailcheck.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ToString(); emailcheck.SelectParameters.Add("emailid", TypeCode.String, this.emailid.Text); emailcheck.SelectCommand = "SELECT * FROM [users] WHERE [emailid] = @emailid"; DataView emailcheckdv = (DataView)emailcheck.Select(DataSourceSelectArguments.Empty);
try { if (emailcheckdv.Count == 0) { if (password.Text == repassword.Text) { execution(fullname.Text, emailid.Text, username.Text, password.Text); } else { Label1.Visible = true; Label1.Text = "Form not completed or password not matched."; } } else { Label1.Visible = true; Label1.Text = "Email ID already Registered."; } } catch (System.Data.SqlClient.SqlException ex_msg) { string msg = "Error occured while login."; msg += ex_msg.Message; Label1.Visible = true; Label1.Text = msg; } } else { username.Text = null; password.Text = null; Label1.Visible = true; Label1.Text = "User already exist, please use different username."; } } catch (System.Data.SqlClient.SqlException ex_msg) { string msg = "Error occured while login."; msg += ex_msg.Message; Label1.Visible = true; Label1.Text = msg; } finally { //Here will be fially elements } }
public string GetConnectionString() { return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString; }
public void execution(string fullname, string emailid, string username, string password) { SqlConnection myConnection = new SqlConnection(GetConnectionString()); SqlCommand storeimage = new SqlCommand("INSERT INTO users (fullname,emailid,username,password) VALUES (@fullname,@emailid,@username,@password)", myConnection); storeimage.Parameters.Add("@fullname", SqlDbType.VarChar, 50).Value = fullname; storeimage.Parameters.Add("@emailid", SqlDbType.VarChar, 50).Value = emailid; storeimage.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = username; storeimage.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = password;
try { myConnection.Open(); storeimage.ExecuteNonQuery(); sendmail(fullname, emailid, username, password); Label1.Visible = true; Label1.Text = "User Created."; } catch { //catch block goes here } }
public void sendmail(string fullname, string emailid, string username, string password) { string siteurl = "http://www.yourdomain.com/login/Activation.aspx"; string smsg = "New Registration on our website, find your details below:<br>"; smsg += "<br><b>Name: </b>" + fullname; smsg += "<br><b>Username: </b>" + username; smsg += "<br><b>Password: </b>" + password; smsg += "<br><b>Your account is not activated still, please activate it by clicking here: </b>"; smsg += "<br><b>Title of Post: </b><br>" + "<a href=" + siteurl + "?username=" + username + "></a>"; smsg += "<br><br><br><br>"; smsg += "<b>Administrator";
MailMessage message = new MailMessage(); try { message.To.Add(new MailAddress(emailid)); message.From = new MailAddress("youremailaddress");
message.Subject = "yoursubject"; message.Body = smsg; message.IsBodyHtml = true; SmtpClient client = new SmtpClient(); client.Port = 25; // Gmail works on this port 587 client.Host = "smtp.gmail.com"; System.Net.NetworkCredential nc = new System.Net.NetworkCredential("youremailaddress", "password"); client.EnableSsl = true; client.UseDefaultCredentials = false; client.Credentials = nc; client.Send(message); } catch { //catch block goes here } }
|