Posted by : Anwar Hossain
Category : DataTable example in ASP.Net C#

Store DataTable in session using ASP.Net C Sharp

Dear viewer's in this tutorial I will show how to accumulate datatable in session and can be retrived and displayed this session vlaues in ASP.Net C # as requirment. In this example I have created a DataTable and after filling data to DataTable then I have stored datatable in session. For clear concept I have also used a gridview to display the DataTable information from session .

Store DataTable in session using C#

HTML

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>DataTable and Session </title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="Studentlist" CssClass="Grid" runat="server" AutoGenerateColumns="False"

            CellPadding="3" GridLines="Horizontal" Width="55%" BackColor="White"

            BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px">

            <AlternatingRowStyle BackColor="#F7F7F7" />

            <Columns>

                <asp:BoundField DataField="StudentName" HeaderText="StudentName" />

                <asp:BoundField DataField="Address" HeaderText="Address" />

                <asp:BoundField DataField="Phone" HeaderText="Phone" />

                <asp:BoundField DataField="JoingDate" HeaderText="JoingDate" />

            </Columns>

            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />

            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />

            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />

            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />

            <SortedAscendingCellStyle BackColor="#F4F4FD" />

            <SortedAscendingHeaderStyle BackColor="#5A4C9D" />

            <SortedDescendingCellStyle BackColor="#D8D8F0" />

            <SortedDescendingHeaderStyle BackColor="#3E3277" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

 

public partial class DataTableandSession : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

        Session["StudentList"] = GetStudentList();

        Studentlist.DataSource = Session["StudentList"];

        Studentlist.DataBind();

 

 

    }

  

    static DataTable GetStudentList()

    {

 

        //Created virtual data Storage

 

        DataTable table = new DataTable();

        table.Columns.Add("StudentName", typeof(string));

        table.Columns.Add("Address", typeof(string));

        table.Columns.Add("Phone", typeof(string));

        table.Columns.Add("JoingDate", typeof(DateTime));

 

 

        // Now have created some row to fill the the datatable

 

        table.Rows.Add("Samima Nasim", "Dhaka", "01856587548", "11/5/2010");

        table.Rows.Add("Ahsanul Kabir", "Chittagong", "01856587548", "12/ 5/2204");

        table.Rows.Add("Nirub Ashasan", "Sylhet", "01856587544", "11/10/ 2008");

        table.Rows.Add("Zaman Ahmed", "Rajsahi", "01856587878", "11 /15/ 2003");

        table.Rows.Add("Habibur Rahman", "Barisal", "01856587548", "11 /5 / 2010");

 

        return table;

    }

 

  

}

Out Put

DataTable_and_Session_in_CSharp



Realted Article Headline

Store DataTable in session using ASP.Net C Sharp
How to Create DataTable using ASP.NET C#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection