top of page

How to Create Complete Login and Registration System in C# 2020 | C# Tutorial SQL Server

Updated: Dec 29, 2024

In This blog will create a login and registration form with a database in a C# Web Form application. This application has three forms, login, registration, and home. Users first register themselves, then log in to their account and see the welcome message on the home page.




Create Complete Login Form



Create Complete Register Form






This blogs will help you to easy create Login form to make users login to web application , create register form for register new users and home form as default form .


This form is suing c# web application and insert , retrieve Data from SQL server database.


1 - Declare System classes to run SQL commands


using System.Data;

using System.Data.SqlClient;

using System.Configuration;


this Methods for using Sql Query codes inside Asp.net Code Page.


2 - Create connetion to SQL Server.


string constr = ConfigurationManager.ConnectionStrings["con"].ToString();


this code to open connection between web application and SQL server .


3 - Write Code inside Login Button (Loginbtn_Click)


try

{

SqlConnection con = new SqlConnection(constr);

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "sp_Login";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@email", emailtxt.Text.ToString());

cmd.Parameters.AddWithValue("@password", passwordtxt.Text.ToString());

cmd.Connection = con;

con.Open();

SqlDataReader reader = cmd.ExecuteReader();

if (reader.Read())

{

Session["email"] = emailtxt.Text.ToString();

Label1.Text = "Login Successful!";


reader.Close();

con.Close();

Session["id"] = emailtxt.Text;

Response.Redirect("~/Home.aspx");

Session.RemoveAll();

}

else

{

Label1.Text = "Invalid credentials";

}


reader.Close();


con.Close();


}

this code is to check useremail and password is correct or not , if its correct it will redirect the user to home page , if not it will show him Invalid credentials.


4 - Write Code inside Register Button (signupbtn_Click)


SqlConnection CONS = new SqlConnection(constr);

SqlCommand cmd = new SqlCommand();

cmd.Connection = CONS;

cmd.CommandText = "sp_register";

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@name", fnametxt.Text);

cmd.Parameters.AddWithValue("@email", emailtxt.Text);

cmd.Parameters.AddWithValue("@password", passwordtxt.Text);

CONS.Open();

int i = cmd.ExecuteNonQuery();

if (i > 0)

{

Literal1.Text = "you register sucess";

}

else

{

Literal1.Text = "you must fill the missing ";

}

This code to insert new users in SQL database .




Summary


From the preceding all examples we have learned how to Create Login and Register Form . I hope you understand it.

Comments


bottom of page