In
this article we will see about login page in dot net. Here I have created
simple login page to understand how to create it on web page and how to get
data from database for username and password. Here I have used validation
control to validate login page and check for user name and pass in database if
not registered then will generate error for Register First otherwise go to
Welcome page. To
Create Login Page Use following Code and table Script to Create in your
Web application.
Create
table and name it "Users":-
CREATE TABLE [dbo].[Users]( [UserName] [nvarchar](50) NOT NULL, [Password] [nvarchar](50) NOT NULL ) ON [PRIMARY] GO
Default.aspx:-
<div> <fieldset> <legend>Login</legend> <div class='container'> <asp:Label ID="Name" runat="server" Text="UserName:" CssClass="lbl" /> <br /> <asp:TextBox ID="txtUserName" runat="server" Height="22px" /> <asp:RequiredFieldValidator ID="RV1" runat="server" ControlToValidate="txtUserName" ErrorMessage="Please Enter User Name" SetFocusOnError="True">*</asp:RequiredFieldValidator><br /> </div> <div class='container'> <asp:Label ID="lblPwd" runat="server" Text="Password:" CssClass="lbl" /> <br /> <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" CssClass="pwd" Height="22px" /> <asp:RequiredFieldValidator ID="RV2" runat="server" ControlToValidate="txtPwd" ErrorMessage="Your Password" SetFocusOnError="True">*</asp:RequiredFieldValidator><br /> </div> <div class='container'> <asp:Button ID="btnLogIn" runat="server" Text="Sign In" OnClick="btnLogIn_Click" /> </div> <div class='container'> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/ForgotPassword.aspx">Forgot Password ?</asp:HyperLink> <br /> </div> <div class='short_explanation'> New User ? <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="~/Default.aspx">SignUp !</asp:HyperLink> </div> <asp:ValidationSummary ID="ValidationSummary1" runat="server" CssClass="error" /> <br /> <br /> <asp:Label ID="lblMsg" runat="server" Text="" CssClass="lbl" /> </fieldset> </div>
Default.aspx.cs:-
protected void btnLogIn_Click(object sender, EventArgs e) { //Create Connection String And SQL Statement string strCon = ConfigurationManager.ConnectionStrings["DBCS_New"].ConnectionString; string strSelect = "SELECT COUNT(*) FROM Users WHERE UserName = @Username AND Password = @Password"; SqlConnection con = new SqlConnection(strCon); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = strSelect; SqlParameter username = new SqlParameter("@Username", SqlDbType.VarChar, 50); username.Value = txtUserName.Text.Trim().ToString(); cmd.Parameters.Add(username); SqlParameter password = new SqlParameter("@Password", SqlDbType.VarChar, 50); password.Value = txtPwd.Text.Trim().ToString(); cmd.Parameters.Add(password); con.Open(); int result = (Int32)cmd.ExecuteScalar(); con.Close(); if (result >= 1) { Response.Write("WelCome"); } else lblMsg.Text = "Incorrect Username or Password"; }
Now
create you Connection and run your Web application and Enter your Username and
Password to login your Webpage.
Thanks for comments.....