Trouble adding new row to DataTable (C#)

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2007
Posts: 1
Reputation: BoarderX is an unknown quantity at this point 
Solved Threads: 0
BoarderX BoarderX is offline Offline
Newbie Poster

Trouble adding new row to DataTable (C#)

 
0
  #1
Jul 9th, 2007
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 2
Reputation: upswing is an unknown quantity at this point 
Solved Threads: 0
upswing upswing is offline Offline
Newbie Poster

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

 
0
  #2
Jul 31st, 2007
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?



Originally Posted by BoarderX View 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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 2
Reputation: upswing is an unknown quantity at this point 
Solved Threads: 0
upswing upswing is offline Offline
Newbie Poster

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

 
0
  #3
Aug 1st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: Rmez is an unknown quantity at this point 
Solved Threads: 1
Rmez Rmez is offline Offline
Newbie Poster

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

 
0
  #4
Aug 13th, 2007
Originally Posted by BoarderX View 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:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC