SIGN UP MEMBER LOGIN:    
ARTICLE

Simple User Login in ASP.NET using C#

Posted by Rohatash Kumar Articles | ASP.NET Programming June 09, 2011
In this article we will create a registration form, a login form and a form for checking for existing email.
Reader Level:
Download Files:
 


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:

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.

change11.gif

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.


change12.gif

 

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:

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.

login4.gif

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:


login3.gif

 

Figure4

 

Now enter the correct username and password:


login5.gif

 

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:

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.

check13.gif

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:


check14.gif

 

Figure 7

 

Then click on the Check existence button.


check15.gif

 

Figure8

 

Now enter an existing email id and click on the Check existence button.


check16.gif

 

Figure 9


You can also download the attachment and test it yourself.

Login to add your contents and source code to this article
Article Extensions
Contents added by aldo abriz on May 25, 2012
thanks
share this article :
post comment
 

Why do you leave the conStr open and not close it on each button event?

Posted by Kieran Callaghan May 21, 2012

Thanks

Posted by Rohatash Kumar Nov 17, 2011

Nice Work ....

Posted by srikanth kumar Oct 29, 2011

Thank's micalgray

Posted by Rohatash Kumar Jul 27, 2011

Good Work. Rohatash

Posted by Micalgray Jun 14, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor