Creating Login Form In ASP.NET

Today, we are going to make a login page, just another simple web form. You can easily implement this concept anywhere in .NET applications. I have used some controls to build the login form in a .NET application.
  • Text Box
  • Label and Button
Please follow these steps to make this application.

Step 1

First, open your Visual Studio -> File -> New -> Website -> ASP.NET Empty website and click OK. Now, open Solution Explorer and go to Add New Item -> Web form -> click Add.

Step 2

Now, make a Login page like the one given below.
.aspx or design page (front-end)
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head runat="server">  
  4.     <title>Login Form</title>  
  5. </head>  
  6.   
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.         <div>  
  10.             <table>  
  11.                 <tr>  
  12.                     <td> Username: </td>  
  13.                     <td>  
  14.                         <asp:TextBox ID="txtUserName" runat="server" />  
  15.                         <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" /> </td>  
  16.                 </tr>  
  17.                 <tr>  
  18.                     <td> Password: </td>  
  19.                     <td>  
  20.                         <asp:TextBox ID="txtPWD" runat="server" TextMode="Password" />  
  21.                         <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password" /> </td>  
  22.                 </tr>  
  23.                 <tr>  
  24.                     <td> </td>  
  25.                     <td>  
  26.                         <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> </td>  
  27.                 </tr>  
  28.             </table>  
  29.         </div>  
  30.     </form>  
  31. </body>  
  32.   
  33. </html>  
Now, create an event for click button, such as -  onclick="<>".
  1. onclick="btnSubmit_Click"  
So, our button tag will be like <asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />. And in login.aspx.cs page, we need to write the following code.
C# Code --Code behind(Aspx.cs)
  1. using System;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Configuration;  
After adding the namespaces, write the following code in code behind.
  1. protected void btnSubmit_Click(object sender, EventArgs e) {  
  2.     SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);  
  3.     con.Open();  
  4.     SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password", con);  
  5.     cmd.Parameters.AddWithValue("@username", txtUserName.Text);  
  6.     cmd.Parameters.AddWithValue("@password", txtPWD.Text);  
  7.     SqlDataAdapter da = new SqlDataAdapter(cmd);  
  8.     DataTable dt = new DataTable();  
  9.     da.Fill(dt);  
  10.     if (dt.Rows.Count > 0) {  
  11.         Response.Redirect("Details.aspx");  
  12.     } else {  
  13.         ClientScript.RegisterStartupScript(Page.GetType(), "validation""<script language='javascript'>alert('Invalid Username and Password')</script>");  
  14.     }  
  15. }  
Now, for connecting it with the database, write the database connection string like the following.

Web Config
  1. <connectionStrings>  
  2.     <add name="dbconnection" connectionString="Data Source=BrajeshKr;Integrated Security=true;Initial Catalog=MyDB" /> </connectionStrings>
That's it. Now, our Login page is ready to be executed. You can download the code for this application and run that on your Visual Studio.