Simple User Login in
ASP.NET using C#
Introduction
In this article we will create a registration form, a login form and a form for checking for existing email. We will create a table and stored procedure in a SQL database.
1. Registration Form
Step-1
Create Table
Now create a table in a SQL Server database with username, password and email fields. The table looks like this.
create table registrationtab
(
Username varchar(100),
Email varchar(100),
Password varchar(20)
)
Step-2
Create stored procedure
Now create a stored procedure for registration. That means the values will be inserted into the table using the stored procedure. The stored procedure looks like:
Step-2
Create stored procedure
Now create a stored procedure for registration. That means the values will be inserted into the table using the stored procedure. The stored procedure looks like:
create procedure [dbo].[storlogin134]
(
@username varchar(40),
@email varchar(50),
@password varchar(20)
)
as
insert into registrationtab values(@username,@email,@password )
Step-3
Create form for registration
Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.
Figure1
Now double-click on the register me button and add following code.
Step-3
Create form for registration
Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.
Figure1
Now double-click on the register me button and add following code.
protected void Buttonregisterme_Click(object sender, EventArgs e)
{
string strcon ="Data Source=.;uid=sa;pwd=Password$2;database=master";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("storlogin134", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);
SqlParameter p2 = new SqlParameter("email", TextBoxemail.Text);
SqlParameter p3 = new SqlParameter("password", TextBoxpassword.Text);
com.Parameters.Add(p1);
com.Parameters.Add(p2);
com.Parameters.Add(p3);
con.Open();
com.ExecuteNonQuery();
Labelinfo.Text = "registered successful.";
}
Now run the application and enter the
username, email and password and then click on the register me button to save
the values to the database.
Figure 2
Open the database and
check the registrationtab table.
2. Login form
Step-4
Create stored procedure
Now create a stored procedure for login. That means the values will be selected from the table using a stored procedure. The stored procedure looks like:
2. Login form
Step-4
Create stored procedure
Now create a stored procedure for login. That means the values will be selected from the table using a stored procedure. The stored procedure looks like:
create PROCEDURE CheckUser
(
@username as varchar(50),
@password as varchar(50)
)
AS
SELECT * FROM registrationtab WHERE username=@username AND password=@password
Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.
Figure3
Step-5
Create form for login
Now create a form in ASP.Net with the username and password field which are defined in the table. The form looks like the following figure.
Figure 4
Now double-click on the login button and add the following code.
Now create a form in ASP.Net with the following fields defined in the table. The form looks like the following figure.
Figure3
Step-5
Create form for login
Now create a form in ASP.Net with the username and password field which are defined in the table. The form looks like the following figure.
Figure 4
Now double-click on the login button and add the following code.
protected void Buttonlogin_Click(object sender, EventArgs e)
{
string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("CheckUser", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("username", TextBoxusername.Text);
SqlParameter p2 = new SqlParameter("password", TextBoxpassword.Text);
com.Parameters.Add(p1);
com.Parameters.Add(p2);
con.Open();
SqlDataReader rd = com.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
Labelinfo.Text = "Login successful.";
}
else
{
Labelinfo.Text = "Invalid username or
password.";
}
}
Now run the application and enter the
username and password and then click on the login button to retrieve the values
from the database. Now suppose we enter the wrong username and password,
as in:
Figure4
Now enter the correct username and
password:
Figure 5
3. check existence
Step-6
Check email Existence
Now create a stored procedure for checking the existence of email. The stored procedure looks like:
Now create a stored procedure for checking the existence of email. The stored procedure looks like:
create PROCEDURE existance
(
@email as varchar(50)
)
AS
SELECT * FROM registrationtab WHERE email= @email
Now create a form in ASP.Net with the email field defined in the table. The form looks like the following figure.
Figure 6
Step-7
Now double-click on the login button and add the following code:
Now create a form in ASP.Net with the email field defined in the table. The form looks like the following figure.
Figure 6
Step-7
Now double-click on the login button and add the following code:
protected void Buttonchekexistance_Click(object sender, EventArgs e)
{
string strcon = "Data Source=.;uid=sa;pwd=Password$2;database=master";
SqlConnection con = new SqlConnection(strcon);
SqlCommand com = new SqlCommand("existance", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("email",
TextBoxemail.Text);
com.Parameters.Add(p1);
con.Open();
SqlDataReader rd = com.ExecuteReader();
if (rd.HasRows)
{
rd.Read();
this.Labelinfo.ForeColor =
System.Drawing.Color.Red;
this.Labelinfo.Text = "already Exist!";
}
else
{
this.Labelinfo.Text = "you can login now";
this.TextBoxusername.Text = "";
this.TextBoxpassword.Text = "";
this.TextBoxemail.Text = "";
}
}
Now run the application and enter the
username, email and password to check for the existence of the emailid in the
database. Suppose we enter a new email id:
Figure 7
Then click on the Check existence button.
Figure8
Now enter an existing email id and
click on the Check existence button.
Figure 9
You can also download the attachment and test it
yourself.
No comments:
Post a Comment