944,131 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 26155
  • C# RSS
Jul 9th, 2007
0

Trouble adding new row to DataTable (C#)

Expand Post »
I have been following this tutorial (http://msdn2.microsoft.com/en-us/lib...63(vs.80).aspx ) and so far everything has gone well. I am running Visual Studio 2005 and SQL Server 2005. I can run queries just fine. However, when I try to update (insert a new row), my Customers table in my SampleDatabaseDataSet will not commit any new changes.

I am using this documentation for adding rows: http://msdn2.microsoft.com/en-us/lib...34(VS.80).aspx

Here is my code:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace TestApp
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. SampleDatabaseDataSet.CustomersRow newCustomersRow =
  22. sampleDatabaseDataSet.Customers.NewCustomersRow();
  23.  
  24. newCustomersRow.CustomerID = "AABCD";
  25. newCustomersRow.CompanyName = "AAA Works";
  26.  
  27. sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);
  28.  
  29. // Why won't these changes commit?
  30. customersTableAdapter.Update(newCustomersRow);
  31. sampleDatabaseDataSet.Customers.AcceptChanges();
  32. customersBindingSource.EndEdit();
  33.  
  34. this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
  35. }
  36. }
  37. }

When this runs, the form displays that the row was added correctly. However, after the program is finished executing, the data is not written to the table. The changes don't really commit. Any ideas? Thanks.
Last edited by BoarderX; Jul 9th, 2007 at 3:00 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BoarderX is offline Offline
1 posts
since Jul 2007
Jul 31st, 2007
0

Re: Trouble adding new row to DataTable (C#)

I have the same problem can someone help

here is the test code:

deletetable is two columns only

ACRM.deletetableDataTable table = new ACRM.deletetableDataTable();

DataRowCollection rc;
DataRow newRow;
rc = table.Rows;
newRow = table.NewRow();
newRow[1] = "hello";
rc.Add(newRow);

the code runs but nothin gets written .. what are we doig wrong?



Click to Expand / Collapse  Quote originally posted by BoarderX ...
I have been following this tutorial (http://msdn2.microsoft.com/en-us/lib...63(vs.80).aspx ) and so far everything has gone well. I am running Visual Studio 2005 and SQL Server 2005. I can run queries just fine. However, when I try to update (insert a new row), my Customers table in my SampleDatabaseDataSet will not commit any new changes.

I am using this documentation for adding rows: http://msdn2.microsoft.com/en-us/lib...34(VS.80).aspx

Here is my code:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace TestApp
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. SampleDatabaseDataSet.CustomersRow newCustomersRow =
  22. sampleDatabaseDataSet.Customers.NewCustomersRow();
  23.  
  24. newCustomersRow.CustomerID = "AABCD";
  25. newCustomersRow.CompanyName = "AAA Works";
  26.  
  27. sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);
  28.  
  29. // Why won't these changes commit?
  30. customersTableAdapter.Update(newCustomersRow);
  31. sampleDatabaseDataSet.Customers.AcceptChanges();
  32. customersBindingSource.EndEdit();
  33.  
  34. this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
  35. }
  36. }
  37. }

When this runs, the form displays that the row was added correctly. However, after the program is finished executing, the data is not written to the table. The changes don't really commit. Any ideas? Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
upswing is offline Offline
2 posts
since Jul 2007
Aug 1st, 2007
0

Re: Trouble adding new row to DataTable (C#)

The problem i my code was that I did not update the table adapter revised code that works is shown
ACRMTableAdapters.deletetableTableAdapter deletetable_TA = new ACRMTableAdapters.deletetableTableAdapter();
ACRM ACRM1 = new ACRM();

ACRM.deletetableRow newRow = ACRM1.deletetable.NewdeletetableRow();
newRow.test1 = "ALFKI3";
ACRM1.deletetable.Rows.Add(newRow);
deletetable_TA.Update(newRow);

I hope this helps someone
Reputation Points: 10
Solved Threads: 0
Newbie Poster
upswing is offline Offline
2 posts
since Jul 2007
Aug 13th, 2007
0

Re: Trouble adding new row to DataTable (C#)

Click to Expand / Collapse  Quote originally posted by BoarderX ...
I have been following this tutorial (http://msdn2.microsoft.com/en-us/lib...63(vs.80).aspx ) and so far everything has gone well. I am running Visual Studio 2005 and SQL Server 2005. I can run queries just fine. However, when I try to update (insert a new row), my Customers table in my SampleDatabaseDataSet will not commit any new changes.

I am using this documentation for adding rows: http://msdn2.microsoft.com/en-us/lib...34(VS.80).aspx

Here is my code:

C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Data.SqlClient;
  9.  
  10. namespace TestApp
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void Form1_Load(object sender, EventArgs e)
  20. {
  21. SampleDatabaseDataSet.CustomersRow newCustomersRow =
  22. sampleDatabaseDataSet.Customers.NewCustomersRow();
  23.  
  24. newCustomersRow.CustomerID = "AABCD";
  25. newCustomersRow.CompanyName = "AAA Works";
  26.  
  27. sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);
  28.  
  29. // Why won't these changes commit?
  30. customersTableAdapter.Update(newCustomersRow);
  31. sampleDatabaseDataSet.Customers.AcceptChanges();
  32. customersBindingSource.EndEdit();
  33.  
  34. this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
  35. }
  36. }
  37. }

When this runs, the form displays that the row was added correctly. However, after the program is finished executing, the data is not written to the table. The changes don't really commit. Any ideas? Thanks.
I think you should use your update method to DataTable "Customers" insted of DataRow.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Rmez is offline Offline
2 posts
since Jun 2007
Oct 20th, 2010
0
Re: Trouble adding new row to DataTable (C#)
Easy!!!


var itemsSelected = dtSource[0][0];
dt.Rows.Add(AddRow(dt, itemsSelected));


private DataRow AddRow(DataTable dt, DataRow dr)
{
DataRow newRow = dt.NewRow();
newRow["Id"] = dr["ID"];
newRow["Order"] = dr["Order"];

return newRow;
}

http://www.ericklan.com/

Click to Expand / Collapse  Quote originally posted by upswing ...
I have the same problem can someone help

here is the test code:

deletetable is two columns only

ACRM.deletetableDataTable table = new ACRM.deletetableDataTable();

DataRowCollection rc;
DataRow newRow;
rc = table.Rows;
newRow = table.NewRow();
newRow[1] = "hello";
rc.Add(newRow);

the code runs but nothin gets written .. what are we doig wrong?
Last edited by ericklan; Oct 20th, 2010 at 8:53 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ericklan is offline Offline
1 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Logic problem with switch and case.
Next Thread in C# Forum Timeline: Help with Buttons!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC