Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.net DataTable doesn't store new rows
    text
    copied!<p>In my simple starter asp page I create a DataTable and populate it with two rows. The web site allows users to add new rows. My problem is the DataTable doesn't save the information. I've stepped through the code and the row gets added but the next time a row is added it's not there, only the original two and the newest one to get entered. </p> <p>I have gotten around this, in an inelegant way, but am really curious why the new rows are being saved.</p> <p>My code:</p> <pre><code> protected void Page_Load(object sender, EventArgs e) { _Default.NameList = new DataTable(); DataColumn col = new DataColumn("FirstName", typeof(string)); _Default.NameList.Columns.Add(col); col = new DataColumn("LastName", typeof(string)); _Default.NameList.Columns.Add(col); DataRow row = _Default.NameList.NewRow(); row["FirstName"] = "Jane"; row["LastName"] = "Smith"; _Default.NameList.Rows.Add(row); row = _Default.NameList.NewRow(); row["FirstName"] = "John"; row["LastName"] = "Doe"; _Default.NameList.Rows.Add(row); } protected void AddButton_Click(object sender, EventArgs e) { DataRow row = _Default.NameList.NewRow(); row["FirstName"] = this.TextBox1.Text; row["LastName"] = this.TextBox2.Text; _Default.NameList.Rows.Add(row); _Default.NameList.AcceptChanges(); // I've tried with and without this. } </code></pre> <p>I've tried saving them to GridView control but that seems like quite a bit of work. I'm new to ASP.Net but have done windows programming in C# for the last two years.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload