Showing posts with label Session. Show all posts
Showing posts with label Session. Show all posts

How To Pass DataTable Values To Another DataTable Using Session in Asp.Net C#

Pass DataTable Values To Another DataTable Using Session


Pass One DataTable Values To Another DataTable Or Another Page DataTable Using Session In Asp.Net C#.

                    C# CODING


         DataTable dt = new DataTable();
        Session["Details"] = dt;
        DataTable dt1 = (DataTable)Session["Details"];      
        GridView2.DataSource = dt1;
        GridView2.DataBind();


GridView To Another Page GridView Send Values Using DataSet or DataTable With Session In Asp.Net C#

GridView To Another Page GridView Send Values

One Gridview Display Values Send To Another Page Gridview  Using DataTable Or DataSet With Pass Session & Get Another Page Page_Load  Get Session bind To Gridview In Asp.Net C#.

                                     DEMO




HTML CODING(First Page)

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table><tr><td></td><td><asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" >
     <HeaderStyle BackColor="#FF3300" />
            <RowStyle BackColor="Silver" />
            <AlternatingRowStyle BackColor="#9999FF" />
</asp:GridView></td></tr>
            <tr><td colspan="2">  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Next Page" /></td></tr>
        </table>

        <br />
    </div>
    </form>
</body>
</html>

  
           HTML CODING(Second Page)


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> </title>
</head>
<body>
    <form id="form1" runat="server">
      <div>
           
         <table><tr><td> </td><td>   <asp:GridView ID="GridView1" runat="server">

               <AlternatingRowStyle BackColor="#9999FF" />

                 <HeaderStyle BackColor="#FF3300" />
                      <RowStyle BackColor="#FF99FF" />           
            </asp:GridView>
             </td></tr></table>  
        </div>
    </form>
</body>

</html>



          C# CODING(First Page)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;


public partial class Default4 : System.Web.UI.Page
{
    DataSet ds1;

    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();

        SqlCommand cmd = new SqlCommand("select * from country", con);

        SqlDataAdapter adp = new SqlDataAdapter(cmd);
         ds1 = new DataSet();
        adp.Fill(ds1);

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

    }  
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["ds"] = ds1;
        Response.Redirect("Default3.aspx");
    }
}
  

                C# CODING(Second 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;


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

        DataSet ds2 = new DataSet();
        ds2 = (DataSet)Session["ds"];

        GridView1.DataSource = ds2;
        GridView1.DataBind();
      
    }
           
}


First - Add New Web Form - Insert DataBase Table Values 





Next - Add GridView & Button  From Toolbox in First Page





Next - Select Database Table Values To Bind GridView  & Session Pass DataSet ValuesTo Another Page






Next - Second Page - Add Gridview From Tool Box







Second Page - Page_Load - Get Session Values & Bind To  GridView






Next - Run[F5] - Display GridView Values - Click Button - Send DataSet Values To Next Page 










Redirect Page After Session Timeout Using in Asp.Net C#

Redirect Page After Session Timeout

If Use Session Passing Username Another Page  Without Page Refresh Particular Time (Fixed) Expires Session TimeOut  Go - To Login Page


DEMO




Web.Config Coding


<configuration>


<system.web>


      <sessionState mode="InProc" timeout="1"></sessionState>

    
    </system.web>

 

</configuration>



Global.asax


<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }
   
    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }
       
    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started


        if (Session["Username"] == null)
        {
           
            //Redirect to After Session Timeout  
           
            Response.Redirect("Login.aspx");
           
           

        }
       
 
 
       
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer
        // or SQLServer, the event is not raised.

    }
      
</script>



First - Add  The Session Time Out Duration Add To Web.Config File






Next - Add the Global.asax File 







Show The Global.asax Form







Next -  Go to - Login Page -  Send the Username Using Session  to Next Page








Get the  Username From Menu Form







Here - Add the  Session Values  Above 1 Minutes  Did Not Access  - Session Timeout After Go - To Login Page






OUTPUT


Next -  Go To - Login Page - Username & Password Put To Login  Go To Menu Form 





Next - 1 Minute After Page  Session Expire  Go - To Login Page