how to add a new row in datatable at runtime(values from textboxes) with same object

With something like this:

DataRow newRow = dataSet1.Tables["Customers"].NewRow();

newRow["CustomerID"] = "ALFKI";
newRow["CompanyName"] = "Alfreds Futterkiste";

dataSet1.Tables["Customers"].Rows.Add(newRow);

You add a dataRow to the existing dataTable and then specify what data you want to put in the columns. If you don't have columns specified like the example above you need to create the actual columns too (really only useful if you are creating the dataTable from scratch). If you have populated the dataTable from any type of data source the columns should be named.

I think you are aware of ASP.NET page life cycle. The Page and all objects (controls and other) will be created each time whenever a page is requested. So you need to use the state management - session to persist the DataTable/DataSet instance.

In page_load handler

if(!IsPostBack)
{
   DataTable dt=new DataTable();
   dt.Columns.Add("No");
   dt.Columns.Add("Name");
   Session["dt"]=dt;
 }

And in Button's Click handler

DataTable dt=(DataTable)Sesstion["dt"];
dt.Rows.Add(TextBox1.Text,TextBox2.Text);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.