Beruflich Dokumente
Kultur Dokumente
com
http://www.webcodeexpert.com/2013/08/create-registration-form-and-send.html
Note: I have set the data type of the Is_Approved field to bit and set its default
value to 0 i.e. false. It means whenever a new record is inserted in the table the
Is_Approved will be 0. So the new users need to get their account approved
by clicking on the activation link in their email address.
In the web.config file create the connection string to connect your asp.net
application to the Sql server database as:
<connectionStrings>
<add name="conStr" connectionString="Data Source=lalit;Initial Catalog=MyDataBase;Integrated
Security=True"/>
</connectionStrings>
Note: Replace the Data Source and Initial Catalog(i.e. DataBase Name) as per your application.
Source Code:
Add a page and name it Registration.aspx and design the page as:
<div>
<fieldset style="width:350px;">
<legend>Registeration page</legend>
<table>
<tr>
<td>Name *: </td><td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /><asp:RequiredFieldValidator
ID="rfvName" runat="server" ErrorMessage="Please enter Name"
ControlToValidate="txtName" Display="Dynamic" ForeColor="#FF3300"
SetFocusOnError="True"></asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>Email Id: * </td><td>
<asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="rfvEmailId" runat="server"
ControlToValidate="txtEmailId" Display="Dynamic"
ErrorMessage="Please enter Email Id" ForeColor="Red" SetFocusOnError="True">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rgeEmailId" runat="server"
ControlToValidate="txtEmailId" Display="Dynamic"
ErrorMessage="Please enter valid email id format" ForeColor="Red"
SetFocusOnError="True"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td>Address : </td><td>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Contact No.</td><td>
<asp:TextBox ID="txtContactNo" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td> </td><td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit"
onclick="btnSubmit_Click" /></td>
</tr>
</table>
</fieldset>
</div>
Note: I have also implemented the validation on Name and the Email Id field to ensure they are not left blank
using RequiredFieldValidator validation control and also used RegularExpressionValidator to check for the valid
email address on the Email Id field.
{
MailMessage msg;
SqlCommand cmd = new SqlCommand();
string ActivationUrl = string.Empty;
string emailId = string.Empty;
try
{
cmd = new SqlCommand("insert into Tb_Registration (Name,EmailId,Address,ContactNo) values
(@Name,@EmailId,@Address,@ContactNo) ", con);
cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@EmailId", txtEmailId.Text.Trim());
cmd.Parameters.AddWithValue("@Address", txtAddress.Text.Trim());
cmd.Parameters.AddWithValue("@ContactNo", txtContactNo.Text.Trim());
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
//Sending activation link in the email
msg = new MailMessage();
SmtpClient smtp = new SmtpClient();
emailId = txtEmailId.Text.Trim();
//sender email address
msg.From = new MailAddress("YourGmailEmailId@gmail.com");
//Receiver email address
msg.To.Add(emailId);
msg.Subject = "Confirmation email for account activation";
//For testing replace the local host path with your lost host path and while making online replace with your
website domain name
ActivationUrl = Server.HtmlEncode("http://localhost:8665/MySampleApplication/ActivateAccount.aspx?
UserID=" + FetchUserId(emailId) + "&EmailId=" + emailId);
cmd.Parameters.AddWithValue("@EmailId", emailId);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string UserID = Convert.ToString(cmd.ExecuteScalar());
con.Close();
cmd.Dispose();
return UserID;
}
Add a new page and name it ActivateAccount.aspx. This page will be opened when new registered user
click on the activate account link in his email. On the page load it will check email id and user id from the
query string and then update the "Is_Approved" column to 1 i.e. True. Then you can redirect him to your
login page to log in.
{
if (!Page.IsPostBack)
{
ActivateMyAccount();
}
}
if ((!string.IsNullOrEmpty(Request.QueryString["UserID"])) &
(!string.IsNullOrEmpty(Request.QueryString["EmailId"])))
{ //approve account by setting Is_Approved to 1 i.e. True in the sql server table
cmd = new SqlCommand("UPDATE Tb_Registration SET Is_Approved=1 WHERE UserID=@UserID
AND EmailId=@EmailId", con);
cmd.Parameters.AddWithValue("@UserID", Request.QueryString["UserID"]);
cmd.Parameters.AddWithValue("@EmailId", Request.QueryString["EmailId"]);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
Response.Write("You account has been activated. You can Login now! ");
}
}
catch (Exception ex)
{
VB.Net Section:
msg.Body = "Hi " & txtName.Text.Trim() & "!" & vbLf & "Thanks for showing interest and registring in
webcodeexpert.com " & " Please & ActivationUrl & "'>click here to activate your account and enjoy our services. "
& vbLf & "Thanks!"
msg.IsBodyHtml = True
smtp.Credentials = New NetworkCredential("YourGmailEmailId@gmail.com", "YourGmailPassword")
smtp.Port = 587
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
smtp.Send(msg)
clear_controls()
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Confirmation Link to activate
account has been sent to your email address');", True)
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " &
ex.Message.ToString() & "');", True)
Return
Finally
ActivationUrl = String.Empty
emailId = String.Empty
con.Close()
cmd.Dispose()
End Try
End Sub
Add a new page ActivateAccount.aspx. This page will be opened when new registered user click on the
activate account link in his email. On the page load it will check email id and user id from the query string
and then update the "Is_Approved" column to 1 i.e. True. Then you can redirect him to your login page to
log in.
In the code behind file (ActivateAccount.aspx.vb) and write the code on the page load event as:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration