vishal mewara

vishal mewara

  • NA
  • 12
  • 34.3k

I want to show Details of currently logged in user.

Apr 1 2014 1:44 AM
First how to get current logged in employee/user ID
and use it in procedure to display only current logged in user details only
Please Help..


Registered Employee Login Form .aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageHeaderFooter.master" AutoEventWireup="true" CodeFile="Registered Employee Login Form.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHeaderFooter" Runat="Server">
    <table cellpadding="3" style="width: 100%; border: 2px solid #808000">
        <tr>
            <td style="height:30px;text-align:left;" colspan="2">
                <asp:Label ID="LabelLoginFailedMessage" runat="server" Font-Bold="True" Font-Size="Medium" ForeColor="Red"></asp:Label>
            </td>
            <td style="Width: 25%; height: 30px;">
                &nbsp;</td>
        </tr>
        <tr>
            <td style="width: 25%; height:30px;text-align:left;"><h3>Employee Login ID : </h3></td>
            <td style="width: 540px; height: 30px; text-align:left;">
                <asp:TextBox ID="TextBoxRegisteredEmployeeLoginFormUserID" runat="server" Width="150px" Height="20px" TextMode="SingleLine"></asp:TextBox></td>
            <td style="Width: 25%; height: 30px;">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorRegisteredEmployeeLoginFormUserID" runat="server" ControlToValidate="TextBoxRegisteredEmployeeLoginFormUserID" ErrorMessage="Please Enter Employee ID." ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator></td>
        </tr>
        <tr>
            <td style="width: 25%; height: 30px; text-align:left;"><h3>Password : </h3></td>
            <td style="width: 540px; height: 30px; text-align:left;"> <asp:TextBox ID="TextBoxRegisteredEmployeeLoginFormPassword" runat="server" Width="150px" Height="20px" TextMode="Password"></asp:TextBox></td>
            <td style="Width: 25%; height: 30px;">
                <asp:RequiredFieldValidator ID="RequiredFieldValidatorRegisteredEmployeeLoginFormPassword" runat="server" ControlToValidate="TextBoxRegisteredEmployeeLoginFormPassword" ErrorMessage="Please Enter Password." ForeColor="Red" Display="Dynamic" SetFocusOnError="true"></asp:RequiredFieldValidator></td>
        </tr>
        <tr>
            <td>
                <asp:CheckBox ID="CheckBoxRememberMe" runat="server"  Text="Remember me....." OnCheckedChanged="CheckBoxRememberMe_CheckedChanged" ValidationGroup="RememberMe"/></td>
            <td style="height: 49px; text-align:left;" colspan="2" >

                <asp:Button ID="ButtonRegisteredEmployeeLoginFormLogin" runat="server" style="width: 100px; margin-left: 25%; margin-right: 0px" Text="Login" OnClick="ButtonRegisteredEmployeeLoginFormLogin_Click"/>
            </td>
        </tr>
        <tr>
            <td colspan="3">
                <asp:HyperLink ID="HyperLinkNewEmployeeRegisteration" runat="server" ForeColor="#33cc33" Font-Size="Larger" NavigateUrl="~/New Employee/New Employee Registeration Form.aspx" Text="Not An Employee Click Here To Register Your Form."></asp:HyperLink>
            </td>
        </tr>
        <tr>
            <td style="height: 30px; text-align:left;" colspan="3" >
                &nbsp;</td>
        </tr>
        </table>
</asp:Content>

 



Registered Employee Login Form .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.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

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

    }
    protected void ButtonRegisteredEmployeeLoginFormLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (AuthenticateAdministrator(TextBoxRegisteredEmployeeLoginFormUserID.Text, TextBoxRegisteredEmployeeLoginFormPassword.Text))
            {
                FormsAuthentication.RedirectFromLoginPage(TextBoxRegisteredEmployeeLoginFormUserID.Text, CheckBoxRememberMe.Checked);
                Response.Redirect("~/Registered Employee/Registered Employee Page.aspx");
            }
            else
            {
                LabelLoginFailedMessage.Text = "Login Data is are Not Correct Please Try Again.";
            }
        }
    }
    private bool AuthenticateAdministrator(string EmployeeID, string EmployeePassword)
    {
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

        using (SqlConnection Con = new SqlConnection(CS))
        {
            SqlCommand Cmd = new SqlCommand("spEmployeeLogin", Con);
            Cmd.CommandType = System.Data.CommandType.StoredProcedure;

            SqlParameter ParaEmployeeID = new SqlParameter("@EmployeeID", EmployeeID.ToString());
            SqlParameter ParaEmployeePassword = new SqlParameter("@EmployeePassword", EmployeePassword.ToString());

            Cmd.Parameters.Add(ParaEmployeeID);
            Cmd.Parameters.Add(ParaEmployeePassword);

            Con.Open();
            int ReturnCode = Convert.ToInt32(Cmd.ExecuteScalar());
            return ReturnCode == 1;

        }
    }
    protected void CheckBoxRememberMe_CheckedChanged(object sender, EventArgs e)
    {

    }
}



Register Employee Page.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPageHeaderFooter.master" AutoEventWireup="true" CodeFile="Registered Employee Page.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderHeaderFooter" Runat="Server">
    <asp:Panel ID="PanelRegisterEmployeePage" runat="server" BackColor="#cc9900">
        <asp:TextBox ID="TextBoxCurrentloggedInEmployeeID" runat="server" OnTextChanged="TextBoxCurrentloggedInEmployeeID_TextChanged"></asp:TextBox>
        <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
            <RowStyle BackColor="White" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#808080" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView><br /><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </asp:Panel>
</asp:Content>



Register Employee Page.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.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
        using (SqlConnection Con = new SqlConnection(CS))
        {
            Con.Open();
            SqlCommand Cmd = new SqlCommand("spShowDataToLoggedInEmployee", Con);
            Cmd.CommandType = System.Data.CommandType.StoredProcedure;
            SqlDataReader Rdr = Cmd.ExecuteReader();

            GridView1.DataSource = Rdr;
            GridView1.DataBind();

        }
    }
}



Database Files

Employee Details Table

Create Table EmployeeDetails
(EmployeeID Integer Primary Key IDENTITY(0001,1),
EmployeeFirstName Varchar(15) Not Null,
EmployeeMiddleName Varchar(15),
EmployeeLastName Varchar(15),
EmployeeGender Char(6) Not Null,
EmployeeEmailAddress Varchar(40) Not Null,
EmployeeMobileNumber Varchar(50) Not Null,
EmployeeAddress Varchar(100) Not Null,
EmployeeState Char(25) Not Null,
EmployeeCity Char(20) Not Null,
EmployeePinCode Integer Not Null)
Go

Procedure to show logged in employee Data


Create Procedure spShowDataToLoggedInEmployee
as
Begin
Select EmployeeID as [Employee ID],
EmployeeFirstName as [First Name],
EmployeeMiddleName as [Middle Name],
EmployeeLastName as [Last Name],
EmployeeGender as [Gender],
EmployeeEmailAddress as [Email Adderss],
EmployeeMobileNumber as [Mobile Number],
EmployeeAddress as [Address],
EmployeeState as [State],
EmployeeCity as [City],
EmployeePinCode as [Pin Code]
From EmployeeDetails
End



Employee Login Details


Create Table EmployeeLoginDetails
(EmployeeID NVarchar(10) Primary Key, 
EmployeePassword NVarchar(16) Not Null)
Go


Procedure Form Employee Login

Create Procedure spEmployeeLogin
@EmployeeID NVarchar(10),
@EmployeePassword NVarchar(16)

as
Begin
Declare @Count int

Select @Count = COUNT(EmployeeID) from EmployeeLoginDetails
Where EmployeeID = @EmployeeID
AndEmployeePassword = @EmployeePassword

if(@Count = 1)
Begin
Select 1 as ReturnCode
End
Else
Begin
Select -1 as ReturnCode
End
End


Answers (1)