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.
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)
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title>Login Form</title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table>
- <tr>
- <td> Username: </td>
- <td>
- <asp:TextBox ID="txtUserName" runat="server" />
- <asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" /> </td>
- </tr>
- <tr>
- <td> Password: </td>
- <td>
- <asp:TextBox ID="txtPWD" runat="server" TextMode="Password" />
- <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password" /> </td>
- </tr>
- <tr>
- <td> </td>
- <td>
- <asp:Button ID="btnSubmit" runat="server" Text="Submit" /> </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
Now, create an event for click button, such as - onclick="<>".
- 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)
- using System;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
After adding the namespaces, write the following code in code behind.
- protected void btnSubmit_Click(object sender, EventArgs e) {
- SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
- con.Open();
- SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName =@username and Password=@password", con);
- cmd.Parameters.AddWithValue("@username", txtUserName.Text);
- cmd.Parameters.AddWithValue("@password", txtPWD.Text);
- SqlDataAdapter da = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- da.Fill(dt);
- if (dt.Rows.Count > 0) {
- Response.Redirect("Details.aspx");
- } else {
- ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
- }
- }
Now, for connecting it with the database, write the database connection string like the following.
Web Config
- <connectionStrings>
- <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.