How To Create Login Form In Asp.Net
In this post we will learn How To Create Login Form In Asp.Net, Earlier we have learnt How To Create Login Form – CSS3. Here we are using SQL Server as backend.
Table Design:
|
ColumnName
|
DataType
|
|
UserId
|
Int(set identity property=true)
|
|
UserName
|
varchar(50)
|
|
Password
|
varchar(50)
|
Design View:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <link href="Style.css" rel="stylesheet" /> <title>How To Create Login Form In Asp.Net</title> </head> <body> <form id="form1" runat="server"> <div> <h1 style="text-align:center">How To Create Login Form In Asp.Net</h1> <div class="main"> <div class="login"> <p><asp:TextBox ID="uname" runat="server" type="text" placeholder="Username"></asp:TextBox></p> <p><asp:TextBox ID="upass" runat="server" type="password" placeholder="Password"></asp:TextBox></p> <p class="forgot"> <label> <a href="#">Forgot Password ?</a> </label> </p> <p class="submit"> <asp:Button runat="server" ID="submir" type="submit" Text="Login" OnClick="submir_Click" /> </p> </div> <div class="footer"> <p>© 2013 All rights reserved by HighTechnology.in <a href="http://hightechnology.in" target="_blank">HighTechnology.in</a> | Hosting Partner <a href="http://www.grootstech.com" target="_blank">Grootstech Solutions</a> </p> </div> </div> </div> </form> </body> </html>
Code View:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submir_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connnn"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from users where UserName =@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", uname.Text);
cmd.Parameters.AddWithValue("@password", upass.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Redirect("Home.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}
