| Author: Ramya 13 Aug 2008 | Member Level: Gold | Rating: Points: 6 |
This is a sample. You should modify this for your requirement. Let’s say the name of the file is login.aspx.
Step 1: Create a design for the login page. For example, In the html type the following <login.aspx> <%@ Page language="c#" Codebehind=" login.aspx.cs" Inherits="login" %>
<html><body> <form id="Form1" runat="server">
<p align=center>Login Form</P> <p align=center><asp:Label id=”lblMsg” runat="server"></asp:Label></P>
<h4>Login</h4> Username: <asp:TextBox id="txtUname" Runat="server" /> <asp:RequiredFieldValidator id="rfvUname" ControlToValidate="txtUname" ErrorMessage="Enter a valid user name" runat="server" />
<br/> Password: <asp:TextBox id="txtPassword" Runat="server" TextMode="Password" /> <asp:RequiredFieldValidator ControlToValidate="txtPassword" ErrorMessage="Enter a valid password." id="rfvPassword" runat="server" />
<asp:Button id="btnSubmit" Runat="server" Text="Login" onClick="btnSubmit_Click" /> </form> </body></html>
Step 2 : In the Code Behind, write the c# code for the login form <login.aspx.cs>
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SQL; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls;
namespace <namespace name> {
public class login : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lblMsg.Text = "Enter Username and Password to Login"; } }
protected void btnSubmit_Click(Object obj, EventArgs e) { if (Page.IsValid) { // check for username & password in the database SQLConnection conn = new SQLConnection("<connectionstring>");
// Get the row corresponding the given username and password string strSQL = "Select * From <tablename> Where <userid column name>='" + txtUname.Text + "' and <password column name> = '" + txtPassword.Text + "'";
SQLDataSetCommand dsc = new SQLDataSetCommand(strSQL, conn);
// Fill the dataset DataSet ds = new DataSet(); dsc.FillDataSet(ds, "Users");
// if there no entry then the user is invalid if (ds.Tables["Users"].Rows.Count == 0) { lblMsg.Text = "Invalid User Name and Password. Try Again."; } else { lblMsg.Text = "Welcome, " + txtUname.Text + "!!! You have successfully signed in."; } } }
} }
|