Dev

Tuesday, 29 December 2015

Login Page in asp.net


Home.aspx

<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="User name:"></asp:Label>
        <asp:TextBox ID="Text_Username" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="password"></asp:Label>
        <asp:TextBox ID="Text_Pwd" runat="server"></asp:TextBox>
   
        <br />
        <br />
        <asp:Label ID="LabelError" runat="server" Text="Label"></asp:Label>
   
        <br />
        <br />
        <asp:Button ID="Btn_Login" runat="server" Text="Login" OnClick="Btn_Login_Click" />
        <asp:Button ID="Btn_Cancle" runat="server" Text="Cancle" OnClick="Btn_Cancle_Click" />
   
    </div>
    </form>
</body>

</html>



Home.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Btn_Login_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("server=NameofServer;user id=sa; pwd=123; database=MyDatabase ");
        con.Open();
        string query= "SELECT userName, password FROM tbl_User WHERE userName='"+Text_Username.Text+ "'AND pwd='"+Text_Pwd.Text"'";
        SqlCommand com = new SqlCommand(query, con);
        SqlDataReader dr = com.ExecuteReader();
        if(dr.HasRows==true)
        {
            dr.Read();
            if (dr[0].ToString() == Text_Username.Text && dr[1].ToString() == Text_Pwd.Text)
            {
                Response.Redirect("Welcomepage.aspx");
            }
        }
        else{
            LabelError.Text="! Invalid UserName or Password !Try again with user name and password";
       
        }
    }
    protected void Btn_Cancle_Click(object sender, EventArgs e)
    {
        Text_Username.Text = "";
    }
}

No comments:

Post a Comment