how to implement
simple login form to check username and password details of user in database.
If user details exist in database then we need to redirect user to welcome page
otherwise we need to display “Invalid Username/Password”. Mostly it’s common
for all the websites before access the website. I know that many of them feel
it’s very easy but for the people who have started learning .NET they don’t
know how to implement this because of that I decided to write post to help for
the persons who is in need with this requirement.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Login
Form</title>
</head>
<body>
<form id="form1" runat="server">
Username:
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please
enter Username"ControlToValidate="txtUserName" runat="server" />
Password:
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD"
ErrorMessage="Please
enter Password"/>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</form>
</body>
</html>
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con
= newSqlConnection("
your connection string");
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("User..aspx",false);
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid
Username and Password')</script>");
}
}
Hi!
ReplyDeleteThis is a nice article. Thanks for sharing your knowledge. I have read some other links that's also helpful for beginner.
http://www.aspdotnet-suresh.com/2011/12/how-to-create-simple-login-form-using.html
http://www.mindstick.com/Articles/f55de7c4-0b0c-4295-a19b-7dd654755c6b/?Login%20form%20in%20ASP%20Net
http://www.mindstick.com/Articles/c389febe-fceb-4cb9-a4d1-41259346f449/?Login%20Form%20in%20ASP.Net