Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to create login page in asp.net c# using stored procedures.

In this project I have 4 levels of access
1)Admin
2)User
3)Supervisor
4)TechTeam

Please help me how to create Login using stored procedure.

Thanks
Posted
Comments
Ajith K Gatty 5-Jun-14 5:53am    
Hi...here,Developers will help you to fix your errors and i dont think that they will code for you. Try to code. when you are in problem please come back with specific error details. Everyone will be happy to help you.

Identifying the user that is authentication is a very important task in any application development and I have often read forum posts asking how to create the login page using a Stored Procedure. So by considering the above requirement I have decided to write this article to help beginners, students and who wants to learn how to create a Login page using a Stored Procedure.

So let us start with the walkthrough.

First create one table named Emp_Login in the SQL database with two columns username and password:
create table Emp_Login(userName nvarchar(50), Password nvarchar(50))


Insert the One Record in the table as:
Insert into Emp_Login (userName,Password) values ('vijay','yadav')


In the above table the usename column is used to store the User Name and Password is for the user's Password.

Now let us create a Stored Procedure named Emplogin as follows:

Create  procedure Emplogin
(
@Usename Varchar (20),
@Password varchar (10)
)
as
Begin
Select COUNT(*)from Emp_Login where username=@Usename and password=@Password 
End


In the above Stored Procedure named Emplogin the variable @Usename is used for the username and @Password is used for the password. The select query that I have written in the beginning counts the number of users from the table whose name matches with the exact two variables; that is @Usename and @Password which comes from the front end user input page.

Now create the project named Loginapplication or you can specify any name as:
"Start" - "All Programs" - "Microsoft Visual Studio 2012".
"File" - "New Project" - "C#" - "Empty Project" (to avoid adding a master page).
Give the web site a name such as Loginapplication or another as you wish and specify the location.
Then right-click on Solution Explorer - "Add New Item" - Default.aspx page for Login Credential and Welcome.aspx for redirecting after successful login.
Drag and drop two buttons, one Label and two textBoxes on the <form> section of the Default aspx page.
Then the <form> section of the Default aspx page looks as in the following:

Default page.png

Then double-click on btn2 and write the following code in the Button_click function as:

 protected void Button2_Click (objectsender, EventArgs e)
 {
     connection();
     query = "Emplogin";   //stored procedure Name
     SqlCommand com = new  SqlCommand(query, con);
     com.CommandType= CommandType.StoredProcedure;
     com.Parameters.AddWithValue("@Usename", TextBox1.Text.ToString());   //for username 
     com.Parameters.AddWithValue("@Password", TextBox2.Text.ToString());  //for password
 
     int usercount = (Int32)com.ExecuteScalar();// for taking single value
 
     if (usercount == 1)  // comparing users from table 
     {
         Response.Redirect("Welcome.aspx");  //for sucsseful login
     }
     else
     {
         con.Close();
         Label1.Text = "Invalid User Name or Password";  //for invalid login
     }
}


The most important part of the preceding code is the If condition part, I have used the one integer variable user count which is used to count the single value from the Stored Procedure using ExecuteScalar, if the records are found in the table it returns 1 then the page is redirected to the Welcome page otherwise it gives Invalid user Name or Password.

Now run the application.

our userName is- vijay
and Password is - yadav

Which we are already entered into the table at the creation of the table.
 
Share this answer
 
v2
Comments
Ajith K Gatty 5-Jun-14 6:27am    
really a good work.
[no name] 6-Jun-14 6:34am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900