Creating User Registration Form Example In Asp.Net
In this post i am explaining how to create user registration or signup page in asp.net with sql server database using C# and VB.NET.
For this first of all we need to create a table in sql server database to store user registration data.
I have created a table named Users for this example with columns shown in image below.
We can also Create Users with Membership Provider programmatically Or using CreateUserWizard, You will also need a Login Page
Place textboxes on the form for entering name,username,password,address,email etc.
Set Textmode property of password textbox to password to display * character instead of text entered.
Associate RequiredFieldValidators with textboxes as shown in HTML source.
Place one Label and Button to save the data.
HTML SOURCE OF PAGE
Write below mentioned code in click event of signup button.
C# CODE
VB.NET
Build and run the code.
In this post i am explaining how to create user registration or signup page in asp.net with sql server database using C# and VB.NET.
For this first of all we need to create a table in sql server database to store user registration data.
I have created a table named Users for this example with columns shown in image below.
We can also Create Users with Membership Provider programmatically Or using CreateUserWizard, You will also need a Login Page
Place textboxes on the form for entering name,username,password,address,email etc.
Set Textmode property of password textbox to password to display * character instead of text entered.
Associate RequiredFieldValidators with textboxes as shown in HTML source.
Place one Label and Button to save the data.
HTML SOURCE OF PAGE
1: <table>
2: <tr>
3: <td>First Name:</td>
4: <td><asp:TextBox ID="txtFirstName" runat="server">
5: </asp:TextBox>
6: </td>
7: <td><asp:RequiredFieldValidator ID="rfvFirstName"
8: runat="server"
9: ControlToValidate="txtFirstName"
10: ErrorMessage="First Name can't be left blank"
11: SetFocusOnError="True">*
12: </asp:RequiredFieldValidator>
13: </td>
14: </tr>
15: <tr>
16: <td>Last Name:</td>
17: <td><asp:TextBox ID="txtLastName" runat="server">
18: </asp:TextBox></td>
19: <td><asp:RequiredFieldValidator
20: ID="RequiredFieldValidator1" runat="server"
21: ControlToValidate="txtLastName"
22: ErrorMessage="Last Name can't be left blank"
23: SetFocusOnError="True">*
24: </asp:RequiredFieldValidator>
25: </td></tr>
26:
27: <tr><td>UserName:</td>
28: <td><asp:TextBox ID="txtUserName" runat="server">
29: </asp:TextBox>
30: </td>
31: <td><asp:RequiredFieldValidator
32: ID="rfvUserName"
33: runat="server"
34: ControlToValidate="txtUserName"
35: ErrorMessage="UserName can't be left blank"
36: SetFocusOnError="True">*
37: </asp:RequiredFieldValidator>
38: </td></tr>
39:
40: <tr><td>Password:</td>
41: <td><asp:TextBox ID="txtPwd" runat="server"
42: TextMode="Password">
43: </asp:TextBox>
44: </td>
45: <td><asp:RequiredFieldValidator ID="rfvPwd"
46: runat="server" ControlToValidate="txtPwd"
47: ErrorMessage="Password can't be left blank"
48: SetFocusOnError="True">*
49: </asp:RequiredFieldValidator>
50: </td></tr>
51:
52: <tr><td>Confirm Password:</td>
53: <td><asp:TextBox ID="txtRePwd" runat="server"
54: TextMode="Password">
55: </asp:TextBox>
56: </td>
57: <td><asp:CompareValidator ID="CompareValidator1"
58: runat="server"
59: ControlToCompare="txtRePwd"
60: ControlToValidate="txtPwd"
61: Operator="Equal"
62: ErrorMessage="Password and confirm password do not match"
63: SetFocusOnError="True">
64: </asp:CompareValidator>
65: </td></tr>
66:
67: <tr><td>Gender:</td>
68: <td><asp:RadioButtonList ID="rdoGender"
69: runat="server">
70: <asp:ListItem>Male</asp:ListItem>
71: <asp:ListItem>Female</asp:ListItem>
72: </asp:RadioButtonList>
73: </td>
74: <td><asp:RequiredFieldValidator
75: ID="RequiredFieldValidator2"
76: runat="server"
77: ControlToValidate="rdoGender"
78: ErrorMessage="Gender can't be left blank"
79: SetFocusOnError="True">*
80: </asp:RequiredFieldValidator>
81: </td></tr>
82:
83: <tr><td>Address:</td>
84: <td><asp:TextBox ID="txtAdress" runat="server"
85: TextMode="MultiLine">
86: </asp:TextBox>
87: </td>
88: <td><asp:RequiredFieldValidator ID="rfvAddress"
89: runat="server"
90: ControlToValidate="txtAdress"
91: ErrorMessage="Address can't be left blank"
92: SetFocusOnError="True">*
93: </asp:RequiredFieldValidator>
94: </td></tr>
95:
96: <tr><td>Email ID:</td>
97: <td><asp:TextBox ID="txtEmailID" runat="server">
98: </asp:TextBox>
99: </td>
100: <td><asp:RequiredFieldValidator
101: ID="RequiredFieldValidator3"
102: runat="server"
103: ControlToValidate="txtEmailID"
104: ErrorMessage="Email can't be left blank"
105: SetFocusOnError="True">*
106: </asp:RequiredFieldValidator>
107: </td></tr>
108:
109: <tr><td><asp:Label ID="lblMsg" runat="server">
110: </asp:Label>
111: </td>
112: <td><asp:ValidationSummary ID="ValidationSummary1"
113: runat="server" ShowMessageBox="True"
114: ShowSummary="False"/>
115: </td></tr>
116:
117: <tr><td><asp:Button ID="btnSave" runat="server"
118: Text="Sign Up"
119: onclick="btnSave_Click"/>
120: </td></tr>
121: </table>
Write below mentioned code in click event of signup button.
C# CODE
01
protected
void
btnSave_Click(
object
sender, EventArgs e)
02
{
03
//Create ConnectionString and Inser Statement
04
string
connectionString = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
05
string
insertSql =
"INSERT INTO Users (FirstName,LastName,UserName,Password,Email,Address,Gender)"
06
+
" values (@FirstName,@LastName,@UserName,@Password,@Email,@Address,@Gender)"
;
07
//Create SQL connection
08
SqlConnection con =
new
SqlConnection(connectionString);
09
10
//Create SQL Command And Sql Parameters
11
12
SqlCommand cmd =
new
SqlCommand();
13
cmd.Connection = con;
14
cmd.CommandType = CommandType.Text;
15
cmd.CommandText = insertSql;
16
17
SqlParameter firstName =
new
SqlParameter(
"@FirstName"
, SqlDbType.VarChar, 50);
18
firstName.Value = txtFirstName.Text.ToString();
19
cmd.Parameters.Add(firstName);
20
21
SqlParameter lastName =
new
SqlParameter(
"@LastName"
, SqlDbType.VarChar, 50);
22
lastName.Value = txtLastName.Text.ToString();
23
cmd.Parameters.Add(lastName);
24
25
SqlParameter userName =
new
SqlParameter(
"@UserName"
, SqlDbType.VarChar, 50);
26
userName.Value = txtUserName.Text.ToString();
27
cmd.Parameters.Add(userName);
28
29
SqlParameter pwd =
new
SqlParameter(
"@Password"
, SqlDbType.VarChar, 50);
30
pwd.Value = txtPwd.Text.ToString();
31
cmd.Parameters.Add(pwd);
32
33
SqlParameter email =
new
SqlParameter(
"@Email"
, SqlDbType.VarChar, 50);
34
email.Value = txtEmailID.Text.ToString();
35
cmd.Parameters.Add(email);
36
37
SqlParameter address =
new
SqlParameter(
"@Address"
, SqlDbType.VarChar, 50);
38
address.Value = txtAdress.Text.ToString();
39
cmd.Parameters.Add(address);
40
41
SqlParameter gender =
new
SqlParameter(
"@Gender"
, SqlDbType.VarChar, 10);
42
gender.Value = rdoGender.SelectedItem.ToString();
43
cmd.Parameters.Add(gender);
44
45
46
try
47
{
48
con.Open();
49
cmd.ExecuteNonQuery();
50
lblMsg.Text =
"User Registration successful"
;
51
}
52
catch
(SqlException ex)
53
{
54
string
errorMessage =
"Error in registering user"
;
55
errorMessage += ex.Message;
56
throw
new
Exception(errorMessage);
57
58
}
59
finally
60
{
61
con.Close();
62
}
63
}
VB.NET
01
Protected
Sub
btnSave_Click(sender
As
Object
, e
As
EventArgs)
02
'Create ConnectionString and Inser Statement
03
Dim
connectionString
As
String
= ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString
04
Dim
insertSql
As
String
=
"INSERT INTO Users (FirstName,LastName,UserName,Password,Email,Address,Gender)"
&
" values (@FirstName,@LastName,@UserName,@Password,@Email,@Address,@Gender)"
05
'Create SQL connection
06
Dim
con
As
New
SqlConnection(connectionString)
07
08
'Create SQL Command And Sql Parameters
09
10
Dim
cmd
As
New
SqlCommand()
11
cmd.Connection = con
12
cmd.CommandType = CommandType.Text
13
cmd.CommandText = insertSql
14
15
Dim
firstName
As
New
SqlParameter(
"@FirstName"
, SqlDbType.VarChar, 50)
16
firstName.Value = txtFirstName.Text.ToString()
17
cmd.Parameters.Add(firstName)
18
19
Dim
lastName
As
New
SqlParameter(
"@LastName"
, SqlDbType.VarChar, 50)
20
lastName.Value = txtLastName.Text.ToString()
21
cmd.Parameters.Add(lastName)
22
23
Dim
userName
As
New
SqlParameter(
"@UserName"
, SqlDbType.VarChar, 50)
24
userName.Value = txtUserName.Text.ToString()
25
cmd.Parameters.Add(userName)
26
27
Dim
pwd
As
New
SqlParameter(
"@Password"
, SqlDbType.VarChar, 50)
28
pwd.Value = txtPwd.Text.ToString()
29
cmd.Parameters.Add(pwd)
30
31
Dim
email
As
New
SqlParameter(
"@Email"
, SqlDbType.VarChar, 50)
32
email.Value = txtEmailID.Text.ToString()
33
cmd.Parameters.Add(email)
34
35
Dim
address
As
New
SqlParameter(
"@Address"
, SqlDbType.VarChar, 50)
36
address.Value = txtAdress.Text.ToString()
37
cmd.Parameters.Add(address)
38
39
Dim
gender
As
New
SqlParameter(
"@Gender"
, SqlDbType.VarChar, 10)
40
gender.Value = rdoGender.SelectedItem.ToString()
41
cmd.Parameters.Add(gender)
42
43
44
Try
45
con.Open()
46
cmd.ExecuteNonQuery()
47
lblMsg.Text =
"User Registration successful"
48
Catch
ex
As
SqlException
49
Dim
errorMessage
As
String
=
"Error in registering user"
50
errorMessage += ex.Message
51
52
Throw
New
Exception(errorMessage)
53
Finally
54
con.Close()
55
End
Try
56
End
Sub
Build and run the code.
If you like this post than join us or share
15 comments:
Article is Awesome
But how to implement captcha
Normally Every Gmail, Yahoo, Rediff etc
After Every Registration Provides Captcha Code
which is random code generated even facebook, twitter, eCommerce Site etc. Is it possible to implement.
Thank You
errorMessage += ex.Message;
i don't understand this line .. Message, where was it stored?
@sundeep:
catch (SqlException ex)
{
string errorMessage = "Error in registering user";
errorMessage += ex.Message;
throw new Exception(errorMessage);
}
ex.message will append the error message of sqlexception type, if occured,to errorMessage variable
i tried the c# codes but it is not working.is there any other thing to do before running?the error message is : Compiler Error Message: CS1061: 'ASP.test_aspx' does not contain a definition for 'btnSave_Click' and no extension method 'btnSave_Click' accepting a first argument of type 'ASP.test_aspx' could be found (are you missing a using directive or an assembly reference?)
@Above:Open your page in design mode and double click on save button to generate it's click event in code behind, then write Csharp code in it
i have contact(phone no) column.Here i declare int type. i written code in submit button like,but it gives error as value either too small or too large of INT32. Please correct it
SqlParameter ContactNo = new SqlParameter("@ContactNo", SqlDbType.Int, 50);
ContactNo.Value = convert.toint32(txtcotno.Text.ToString());
cmd.Parameters.Add(ContactNo);
@Above: Create a new column ContactNo in database table With int datatype
Change sql statement to
string insertSql = "INSERT INTO Users (FirstName,LastName,UserName,Password,Email,Address,Gender,ContactNo) values (@FirstName,@LastName,@UserName,@Password,@Email,@Address,@Gender,@ContactNo)";
Create Sql parameter
SqlParameter contactNo = new SqlParameter("@ContactNo", SqlDbType.Int, 10);
contactNo.Value = Convert.ToInt32(txtContactNo.Text.ToString());
cmd.Parameters.Add(contactNo);
This should work fine without any errors
it worked!! thanks a lot!!
Hello,
I have the following problem:
Error in registring userUser does not have permission to perform this action.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Exception: Error in registring userUser does not have permission to perform this action.
Source Error:
Line 70: string errorMessage = "Error in registring user";
Line 71: errorMessage += ex.Message;
Line 72: throw new Exception(errorMessage);
Line 73:
Line 74: }
hi
i have got a null pointer exception while execting
ie use new keyword to create instance of connectionstring...
we need color grid codings ethu kuda upload pana matingala????????????????.....
if i want confirmation code sent to user id who registered currently then how this will be done????/
Hi
thank you for your tutorial but i am getting error messages such as:
City.Value = txtCity.text.ToString();
txtCity does not exist in the current context, and the same message for SqlDbType as well as for cmd.CommandType = CommandType.text; as Command Type
its really nice post but you didn't check already registered user, which is include in database table
Check this article
user registeration
can we give identity property with primary key???
if yes them am facing the problem.....and the error is
"explicit value for identity column in table register can olny be specified when a column list is used and identity_insert is on "
Post a Comment