Saturday 28 December 2013

Login Control To The Data Base


Login Control To The Data Base





Source Code For Login Control

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LoginPage.aspx.cs" Inherits="LoginDetails_LoginPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            text-align: center;
            font-size: x-large;
        }
        .style3
        {
            width: 66px;
        }
        .style4
        {
            width: 66px;
            text-align: right;
            font-family: Arial, Helvetica, sans-serif;
            font-size: small;
        }
    </style>
    <script type="text/javascript">
        window.history.forward();
        function noback() {
            window.history.forward();

        }
        setTimeout("preventBack()", 0);

    </script>
</head>
<body style="width: 316px; height: 127px; margin-left: 307px" önload="noBack();"
    onpageshow="if (event.persisted) noBack();">
    <form id="form1" runat="server">
    <div>
    
        <table cellpadding="0" cellspacing="0" class="style1">
            <tr>
                <td class="style2" colspan="2">
                    <strong>Login Page</strong></td>
            </tr>
            <tr>
                <td class="style4">
                    UserID</td>
                <td>
                    <asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style4">
                    Password</td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    &nbsp;</td>
                <td>
                    <asp:LinkButton ID="lbForgetPassword" runat="server" 
                        onclick="lbForgetPassword_Click">Forget Password</asp:LinkButton>
                </td>
            </tr>
            <tr>
                <td class="style3">
                    <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" 
                        Text="Submit" style="font-weight: 700" />
                </td>
                <td>
                    <asp:Button ID="btnClear" runat="server" Text="Clear" 
                        style="font-weight: 700" />
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


Code Behind To The Control
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 LoginDetails_LoginPage : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=sa123;database=HealthManagement");
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.Cache.SetExpires(DateTime.Now); 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtUserID.Text != "" && txtPassword.Text != "")
        {
            SqlCommand cmd = new SqlCommand("select username,password,usertype from tbluserlogin where username='" + txtUserID.Text + "' and password='" + txtPassword.Text + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            Session["username"] = txtUserID.Text;

            if (dt.Rows.Count > 0)
            {
                string usertype = dt.Rows[0].ItemArray[2].ToString();
                if (usertype == "Admin")
                {
                    Server.Transfer("~/Admin/HomePage.aspx");
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Admin login')", true);
                }
                else if (usertype == "Employee")
                {
                    Server.Transfer("~/Users/HomePage.aspx");
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Employee login')", true);
                }
                else if (usertype == "Doctor")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Doctor login')", true);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('Invalid login details')", true);
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('User name or passwword empty')", true);

        }
    }
    protected void lbForgetPassword_Click(object sender, EventArgs e)
    {
        Server.Transfer("ForgetPasswordPage.aspx");
    }
}



0 comments:

Post a Comment