Oct 8, 2010

How to create Temporary Table in asp.net with compute column in C#




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default3 : System.Web.UI.Page
{
    Int32 tt;  // tt for total it will calculate the total
    DataTable tb = new DataTable(); //create a instance tb of Datatable

    protected void Page_Load(object sender, EventArgs e)
    {
// here we will create a temporary table of columns under ispostback.
        if (IsPostBack == false)
        {
//”ord” is a datatable type which create a “Table” and “c” for datatable columns.
            DataTable ord = new DataTable("Table"); //the “Table” value must be same for
            DataColumn c = new DataColumn();        // always
            ord.Columns.Add(new DataColumn("value", Type.GetType("System.Int32")));
// “value” is a column name with Int32 datatype.
            ord.Columns.Add(new DataColumn("length", Type.GetType("System.Int32")));
// “length” is a column name with Int32 datatype.
            ord.Columns.Add(new DataColumn("breadth", Type.GetType("System.Int32")));
// “total” is a column name with Int32 datatype.
            ord.Columns.Add(new DataColumn("total", Type.GetType("System.Decimal")));
// “total” is a Expression which calculate the following formula
            ord.Columns["total"].Expression = "value/(length*breadth)";
//storing the structure in “ss” name session.
            Session["ss"] = ord;
           
        }      
    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {
        grdview();// will call the grdview method
    }

// following the temporary table with structure
    private void tempdata()
    {
        tb = (DataTable)(Session["ss"]); //getting the structure from session and
        GridView1.DataSource = tb; // storing in “tb” datatable variable and bind with
        GridView1.DataBind(); // gridview
        Int32 a; Int32 i; ;
        a = tb.Rows.Count; // it will count the total records in the temporary table
    

        for (i = 0; i < a; i++)
        {
            tt += Convert.ToInt32(tb.Rows[i][3]);// will sum the each value
        }
        Label1.Text = tt.ToString();// display in label the total values

    }