Beruflich Dokumente
Kultur Dokumente
Categories
Forums
Contact
Database
For this article I have created a new database named LoginDB which contains the following table
named Users in it.
In the above table column UserId is set as primary key and it Identity property is set to true.
Note: The SQL for creating the database is provided in the attached sample code.
HTML Markup
The HTML Markup consists of some TextBox, their corresponding Validators and a Button. Other than
RequiredField Validators theres a CompareValidator to compare passwords and a
RegularExpressionValidator to validate email address.
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th colspan="3">
Registration
</th>
</tr>
<tr>
<td>
Username
</td>
<td>
<asp:TextBox ID="txtUsername" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red"
ControlToValidate="txtUsername"
runat="server" />
</td>
</tr>
<tr>
<td>
Password
</td>
<td>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required" ForeColor="Red"
ControlToValidate="txtPassword"
runat="server" />
</td>
</tr>
<tr>
<td>
Confirm Password
</td>
<td>
<asp:TextBox ID="txtConfirmPassword" runat="server"
TextMode="Password" />
</td>
<td>
<asp:CompareValidator ErrorMessage="Passwords do not match."
ForeColor="Red" ControlToCompare="txtPassword"
ControlToValidate="txtConfirmPassword" runat="server" />
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
<asp:TextBox ID="txtEmail" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ErrorMessage="Required"
Display="Dynamic" ForeColor="Red"
ControlToValidate="txtEmail" runat="server" />
<asp:RegularExpressionValidator runat="server" Display="Dynamic"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtEmail" ForeColor="Red"
ErrorMessage="Invalid email address." />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button Text="Submit" runat="server" OnClick="RegisterUser" />
</td>
<td>
</td>
</tr>
</table>
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
VB.Net
Protected Sub RegisterUser(sender As Object, e As EventArgs)
Dim userId As Integer = 0
Dim constr As String =
ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Insert_User")
Using sda As New SqlDataAdapter()
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Username",
txtUsername.Text.Trim())
cmd.Parameters.AddWithValue("@Password",
txtPassword.Text.Trim())
cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
cmd.Connection = con
con.Open()
userId = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Dim message As String = String.Empty
Select Case userId
Case -1
message = "Username already exists.\nPlease choose a different
username."
Exit Select
Case -2
message = "Supplied email address has already been used."
Exit Select
Case Else
message = "Registration successful.\nUser Id: " +
userId.ToString()
Exit Select
End Select
ClientScript.RegisterStartupScript([GetType](), "alert",
(Convert.ToString("alert('") & message) + "');", True)
End Using
End Sub
Downloads
Related Articles
Send user Confirmation email after Registration with Activation Link in ASP.Net
Here Mudassar Ahmed Khan has explained how to send user confirmation email after
registration with Activation link in ASP.Net using C# and VB.Net. In order to validate the
email address of the user provided during registration, a confirmation email with activation
link in sent to the email address and when user clicks the link, his email address is verified
and his account gets activated.
Comments
thank u sir.
wish u best of luck
Add Comments
You can add your comment about this article using the form below. Make sure you provide a
valid email address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are
Abusive content.
Comment
Security code:
Solutions