Click here to Skip to main content
15,920,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to add one more rows at runtime it will overwrite and displaying the current inserted value. I need to add as a next row and so on.

C#
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 19-Mar-12 0:37am
v2
Comments
chandrashekhar racharla 19-Mar-12 6:44am    
Your question is not clear

Your question isn't really clear but if you're asking what I think you are:

DataTable dt = new DataTable();

This creates a new data table. If you want to re-use an existing one, it needs to be
DataTable dt = something;

... quite possibly
DataTable dt = Session["CurrentData"] as DataTable;


This is pretty basic stuff.
 
Share this answer
 
Your Question is not clear. But, I think you should do like

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
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)));
Session["CurrentData"] = dt;
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
((DataTable)Session["CurrentData"]).Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);
clearfields();
}

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

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

}
}
 
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