Skip to main content

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:

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

Comments

Popular posts from this blog

.NET Core API With Dapper, Repository And UnitOfWork

  Introduction In this tutorial, you will use ASP.NET Core Web API to create a web API that returns a list of brands. This article focuses on creating web API using dapper, repository pattern, and UnitOfWork pattern in .NET 8. I will guide you through the steps by step to create web API. HTTP is not just for serving up web pages. HTTP is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can reach a broad range of clients, including browsers, mobile devices, and traditional desktop applications. ASP.NET Core Web API is a framework for building web APIs on top of the .NET Framework. Creating a New Project in Visual Studio Launch Visual Studio and select Create a New Project. Creating a Core Web API Start Visual Studio and choose Create a new project. In the Create a new project dialog, select ASP.NET Core Web API Click Next. Configuring You...

.Net framework interview questions

What is the .NET Framework? The .NET Framework is a set of technologies that form an integral part of the .NET Platform. It is Microsoft’s managed code programming model for building applications. The .NET Framework has two main components: Common Language Runtime (CLR): The CLR is one of the foundation in the .NET framework and provides a common set of services for applications developed on Microsoft .Net Technologies. .NET Framework class library: The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality. What is CTS (Common Type System)? The common type system (CTS) defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime’s support for cross-language integration. The common type system performs the following functions: ·          Establishes a framework that...

Move Selected Gridview Rows to Another Gridview in Asp.net

Introduction :  Here I will explain how to move or transfer selected checkbox gridview rows to another  grid v iew in asp.net using c#, vb.net or copy one gridview row to another gridview in asp.net using c#, vb.net. Description :  In previous posts I explained Display images in gridview from database in asp.net, Enable/Disable checkbox in gridview based on condition,  asp.net gridview examples  and  bind data to textbox control in gridview ,  Bind data to dropdownlist  in gridview  and many articles relating to  asp.net,  c#, vb.net, c#,vb.net. Now I will explain how to move selected rows from one gridview to another gridview in asp.netusing c#, vb.net. To implement this first we need to write the code in aspx page like this  < html   xmlns ="http://www.w3.org/1999/xhtml"> < head > ...