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

    }




    private void grdview()
    {
            //retreiving the structure from session and after creating the new row
// storing the data in rows as index wise. As we know we cannot store Expression column
// in database name is “total” which calculating the formula.
        tb = (DataTable)(Session["ss"]);
        DataRow r1 = tb.NewRow();
        r1[0] = Convert.ToInt32(txt_value.Text);
        r1[1] = Convert.ToInt32(txt_length.Text);
        r1[2] = Convert.ToInt32(txt_breadth.Text);
        tb.Rows.Add(r1);

        GridView1.DataSource = tb; //now binding the gridview with data.
        GridView1.DataBind();

        Int32 a; Int32 i; ;
        a = tb.Rows.Count;
        tt = 0;
        for (i = 0; i < a; i++)
        {
]);// will sum the each value according to index of the column.
            tt += Convert.ToInt32(tb.Rows[i][3]);
        }
        Label1.Text = tt.ToString(); //will store the Total amount of the Square feet..
   
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            tb = (DataTable)(Session["ss"]);
            tb.Rows.RemoveAt(e.RowIndex); //deleting the records and decreasing the  total
            tempdata(); //calling the tempdata form refresh the output
        }
        catch
        { }

            
    }
}

store the data into database table
*put the below code where u need.
DataTable tb_data = new DataTable();
tb_data = Session(“ss”);    //** here u can store ur all records from session to data table.
savedataintotable();


private void savedataintotable()
{
SqlDataAdapter adp1 = new SqlDataAdapter(“select * from table”, con); //* columnname must be same   
                                                                                                                      table.
SqlCommandBuilder cb = new SqlCommandBuilder(adp1);
DataSet ds1 = new DataSet();
adp1.Fill(ds1);
try {
adp1.Update(tb_data);
} catch (Exception ex) {
}
try
{
tb_data.AcceptChanges()
}
catch (Exception ex) {
}
}
hope it will help u




3 comments:

  1. Thank you very much, help me alot, its a shame you didnt post the next step about storing the data direcly, that would have save me time haha. Thank again and good luck with the new page =).

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete