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. <head runat="server">
  3. <title>Login Form</title>
  4. </head>
  5. <body>
  6. <form id="form1" runat="server">
  7. <div>
  8. <table>
  9. <tr>
  10. <td> Username: </td>
  11. <td>
  12. <asp:TextBox ID="txtUserName" runat="server" />
  13. <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" /> </td>
  14. </tr>
  15. <tr>
  16. <td> Password: </td>
  17. <td>
  18. <asp:TextBox ID="txtPWD" runat="server" TextMode="Password" />
  19. <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password" /> </td>
  20. </tr>
  21. <tr>
  22. <td> </td>
  23. <td>
  24. <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> </td>
  25. </tr>
  26. </table>
  27. </div>
  28. </form>
  29. </body>
  30. </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.