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

Recommended Answers

All 2 Replies

With something like this:

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

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


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

   DataTable dt=new DataTable();

And in Button's Click handler

DataTable dt=(DataTable)Sesstion["dt"];
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.