Login & Register in ASP.Net
4 min readMar 13, 2019
In this tutorial we are going to make a simple asp.net application which demonstrates the login and register process, we will be using Microsoft SQL Server as the backend for this project.
Tools used:
- Microsoft SQL Server, get it from here.
- Microsoft SQL Server Studio, get it from here.
- Microsoft Visual Studio, get it from here.
After completion of this project, I am going to host it in IIS(Internet Information Services), which will be covered in another story.
After the installation and configuration of the tools given, start with the following steps.
Steps
- The first step is to create a database, then create a table to store the basic user information for register and login.
- Next thing is to create a table which stores our data.
- Code for creating the table is given below
create table reglogin(
fname varchar(64),
lname varchar(64),
username varchar(64),
email varchar(128),
pass varchar(128)
)
- Now go to the Microsoft Visual Studio and create a ASP.net web application. Give that project a name and choose the location of the project to be stored. Next select Empty Template in the next screen and click OK.
- Now add 2 web-forms for login and register. This can be achieved by going to project, right click>Add>New Item>Web-form.
- Like the above image add the registration form. Next is the design part, I will be following a basic design for this project, you can design how you want.
- Now Replace the whole code of the Register page after the
<!DOCTYPE Html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Register</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<h2>Register</h2>
<br />
<asp:TextBox ID="TextBox1" placeholder="First Name" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" placeholder="Last Name" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox3" placeholder="Username" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox4" placeholder="Email" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox5" placeholder="Password" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:TextBox ID="TextBox6" placeholder="Re Enter Password" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Register" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
- Now we will look at the Login page
- Next, replace the whole code of the Login page which will come after the
<!DOCTYPE Html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center;">
<h2>Login</h2>
<br />
<asp:TextBox ID="TextBox1" placeholder="Username" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox2" placeholder="Password" runat="server" TextMode="Password"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Login" OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
- We will add the ConnectionStrings to the Web.Config file, which will help in establishing a connection to the database which we have created earlier. The connection string will look something like this.
<connectionStrings><add name="connstr" connectionString="Data Source=NIVIDAI-2;Initial Catalog=sample;Persist Security Info=True;User ID=sa;Password=." providerName="System.Data.SQLClient"/></connectionStrings>
- Now add the below code to Web.Configwhich will help in routing, when we host the application.
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
- Now Web.Config looks like this.
- Now add the connecting string to the cs file of the register which will help to communicate to the database. Add the below string between class and page_load
string ConnectingString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
- Adding the backend code of Register. Copy and paste the below code within the code block of the button.
try
{
SqlConnection conn = new SqlConnection(ConnectingString);
SqlCommand cmd = new SqlCommand("insert into reglogin (fname, lname, username, email, pass" +
"values (@fname, @lname, @username, @email, @pass)", conn);
cmd.Connection = conn;
cmd.Parameters.AddWithValue("@fname", TextBox1.Text);
cmd.Parameters.AddWithValue("@lname", TextBox2.Text);
cmd.Parameters.AddWithValue("@username", TextBox3.Text);
cmd.Parameters.AddWithValue("@email", TextBox4.Text);
if(TextBox5.Text == TextBox6.Text)
{
cmd.Parameters.AddWithValue("@pass", TextBox5.Text);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Passwords do not match!!');</script>", false);
}
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Registered Successfully!!!');</script>", false);
}
catch (Exception ex)
{
ex.ToString();
}
- Now after adding the code, the page will look something like this.
- Now copy and paste the below code in the cs file of the login page.
try
{
string username = TextBox1.Text;
string passw = TextBox2.Text;
SqlConnection conn = new SqlConnection(ConnectingString);
SqlCommand cmd = new SqlCommand("select username, pass from reglogin where username = '" + username + "' and pass = '" + passw + "'", conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Login Succeeded!!!');</script>", false);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "<script>alert('Login Failed!!!');</script>", false);
}
conn.Close();
}
catch (Exception ex)
{
ex.ToString();
}
- After adding the above code the page will look something like this.
After this run the code, and voila you just learnt to implement login and register using asp.net.
Peace ✌️