Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my code........
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void btnsave_Click(object sender, EventArgs e)
    {DataTable dt = new DataTable();
        
        DataRow dr;
       
        dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));



            dr = dt.NewRow();

            dr[0] = TextBox1.Text;
            dr[1] = TextBox2.Text;
            dr[2] = TextBox3.Text;
            dt.Rows.Add(dr);

            Session["CurrentData"] = dt;

            clearfields();
        
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx");

    }
    private void clearfields()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    
    
    }
}
Posted
Updated 18-Mar-12 23:37pm
v2
Comments
Abhinav S 19-Mar-12 5:37am    
Code tags added.
Anuj Banka 19-Mar-12 5:45am    
dt = Session["CurrentData"];
dt.Rows.Add(dr);

before inserting second value.set

dt = Session["CurrentData"];
 
Share this answer
 
Comments
rockpune 19-Mar-12 5:43am    
at which place i add this .......dt = Session["CurrentData"];
Anuj Banka 19-Mar-12 5:47am    
dt = Session["CurrentData"];
dt.Rows.Add(dr);
rockpune 19-Mar-12 5:48am    
i write like this

dt = (DataTable)Session["CurrentData"];

dr = dt.NewRow();

this is correct?
Anuj Banka 19-Mar-12 5:50am    
if (Session["CurrentData"] != null)
{
dt = (DataTable)Session["CurrentData"];
}
dt.Rows.Add(dr);
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 Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }

    protected void btnsave_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        if (Session["CurrentData"] != null)
        {
            dt = (DataTable)Session["CurrentData"];
        }
        DataRow dr;

        dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));



        dr = dt.NewRow();

        dr[0] = TextBox1.Text;
        dr[1] = TextBox2.Text;
        dr[2] = TextBox3.Text;
        dt.Rows.Add(dr);

        Session["CurrentData"] = dt;

        clearfields();


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default3.aspx");

    }
    private void clearfields()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";


    }
}
 
Share this answer
 
Comments
rockpune 19-Mar-12 5:54am    
getting this A column named 'Name' already belongs to this DataTable.
Anuj Banka 19-Mar-12 5:57am    
Then why you are creating datatable again create it only for first time as second time its taking from session or use this code after this code as i suggested.
Sushil Mate 19-Mar-12 6:05am    
Like this.. i haven't tried this.. check if it is working this way

DataTable dt;
if (Session["CurrentData"] != null)
{
dt = (DataTable)Session["CurrentData"];
}
else
{
DataTable dt = new DataTable();
}
Anuj Banka 19-Mar-12 6:10am    
DataTable dt; if (Session["CurrentData"] != null) { dt = (DataTable)Session["CurrentData"]; } else
{
DataTable dt = new DataTable();
dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));
}
You are not restoring the data table that you saved in session.

Try this

C#
private DataTable CreatEmptyDT()
{
    DataTable dt = new DataTable();

    dt.Columns.Add(new System.Data.DataColumn("Name", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("Address", typeof(String)));
    dt.Columns.Add(new System.Data.DataColumn("Cell", typeof(String)));

    return dt;
}

public DataTable CurrentData
{
    get
    {
        object o = Session["CurrentData"];
        if (o == null)
            return CreatEmptyDT();
        else
            return (DataTable)o;
    }
    set { Session["CurrentData"] = value; }
}

protected void btnsave_Click(object sender, EventArgs e)
{
    DataTable dt = CurrentData;

    DataRow dr = dt.NewRow();

    dr[0] = TextBox1.Text;
    dr[1] = TextBox2.Text;
    dr[2] = TextBox3.Text;
    dt.Rows.Add(dr);

    CurrentData = dt;

    clearfields();
}



Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900