Saturday, 11 November 2017

Create Logout page through session veriables

*These namespaces are must be required for SqlServer Connectivity in your web form:-
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

ShowAllData.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ShowAllData.aspx.cs" Inherits="ShowDataOnePage2010.ShowAllData" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <table style="width: 28%">
        <tr>
            <td style="width: 54px">
                <asp:LinkButton ID="LinkButtonLogout" runat="server" Font-Bold="True"
                    ForeColor="Maroon" onclick="LinkButtonLogout_Click">Logout</asp:LinkButton>
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table style="width: 100%">
        <tr>
            <td>
                <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
                    GridLines="None" PageSize="5">
                    <AlternatingRowStyle BackColor="White" />
                    <EditRowStyle BackColor="#7C6F57" />
                    <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#E3EAEB" />
                    <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F8FAFA" />
                    <SortedAscendingHeaderStyle BackColor="#246B61" />
                    <SortedDescendingCellStyle BackColor="#D4DFE1" />
                    <SortedDescendingHeaderStyle BackColor="#15524A" />
                </asp:GridView>
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
</asp:Content>

Web.Config File
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=PRADEEP-HP;Database=Decofloore2;User Id=User_006;password=user006"/>
   </appSettings>
</configuration>

ShowAllData.aspx.cs Page
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;
using System.Configuration;

namespace ShowDataOnePage2010
{
    public partial class ShowAllData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null)
            {
                Response.Redirect("~/LoginSingleData.aspx");
            }
            string StrCon = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection SqlConn = new SqlConnection(StrCon);
            string StrSql = null;
            StrSql = "Select UserId,UserName,Address,PhoneNumber,State,City,Email,DateOfPurchasing,DateOfDelivery from Registration";// where UserName='" + UserName + "'";
            SqlCommand SqlComm = new SqlCommand(StrSql, SqlConn);
            SqlConn.Open();
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader();
            GridView1.DataSource = dr;
            GridView1.DataBind();
            SqlConn.Close();
        }

Logout Button  
protected void LinkButtonLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Session.Remove("UserName");
            Session.RemoveAll();
            Response.Redirect("~/LoginSingleData.aspx");
        }
    }
}

0 comments

Post a Comment