How to create login page in ASP.NET using a stored procedure?

Want to create a Login page in Asp.Net? Learn here how to create a login page using a stored procedure in .net. Get c# codes for Login page using stored procedure here.

Are you looking for code To Create Login page in ASP.NET.
Here I have demonstrated the procedure and code for creating a login page using a Stored Procedure.

Here we will create a Stored procedure in SQLServer and then apply it on the Login page.

The procedure will be as follows:
1. First Create Database in SQL SERVER
2. Then create an sql data table
3. Now create a Stored Procedure

Lets first create the stored procedure something like what I have shown in this SQL script.

C# code for creating Login Page and using a stored procedure to log in

Here is the actual code. You will be required to use the following namespaces:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

The code is as follows:

public partial class Default2 : System.Web.UI.Page
SqlConnection con = new SqlConnection(); //SqlDataReader dr;

protected void Page_Load(object sender, EventArgs e)
con.ConnectionString = ConfigurationManager.ConnectionStrings["gurpreet"].ConnectionString;
if (con.State == ConnectionState.Closed)


protected void Button1_Click(object sender, EventArgs e)
Int32 i = logincheck(name.Text, pwd.Text);
if (i == -1)
Response.Write("<script>alert('Wrong User')</script>");

else if (i == -2)
Response.Write("<script>alert('Wrong Password')</script>");


private Int32 logincheck(string u, string p)
Int32 k = 0;
SqlCommand cmd = new SqlCommand("storeprocedure_login", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@username", SqlDbType.NVarChar,50).Value= u;
cmd.Parameters.Add("@userpwd", SqlDbType.NVarChar, 50).Value = p;
SqlParameter p1 = new SqlParameter("@ret", SqlDbType.Int);
p1.Direction = ParameterDirection.ReturnValue;
//k = Convert.ToInt32(cmd.ExecuteScalar());
//k = Convert.ToInt32(p1.Value);
k = Convert.ToInt32(cmd.Parameters["@ret"].Value);
return k;




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: