954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Trouble adding new row to DataTable (C#)

I have been following this tutorial ( http://msdn2.microsoft.com/en-us/library/ms233763(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/library/5ycd1034(VS.80).aspx

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SampleDatabaseDataSet.CustomersRow newCustomersRow = 
                sampleDatabaseDataSet.Customers.NewCustomersRow();

            newCustomersRow.CustomerID  = "AABCD";
            newCustomersRow.CompanyName = "AAA Works";

            sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);

            // Why won't these changes commit?
            customersTableAdapter.Update(newCustomersRow);
            sampleDatabaseDataSet.Customers.AcceptChanges();
            customersBindingSource.EndEdit();

            this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
        }
    }
}

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.

BoarderX
Newbie Poster
1 post since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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?

I have been following this tutorial ( http://msdn2.microsoft.com/en-us/library/ms233763(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/library/5ycd1034(VS.80).aspx

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SampleDatabaseDataSet.CustomersRow newCustomersRow = 
                sampleDatabaseDataSet.Customers.NewCustomersRow();

            newCustomersRow.CustomerID  = "AABCD";
            newCustomersRow.CompanyName = "AAA Works";

            sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);

            // Why won't these changes commit?
            customersTableAdapter.Update(newCustomersRow);
            sampleDatabaseDataSet.Customers.AcceptChanges();
            customersBindingSource.EndEdit();

            this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
        }
    }
}

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.

upswing
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

upswing
Newbie Poster
2 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

I have been following this tutorial ( http://msdn2.microsoft.com/en-us/library/ms233763(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/library/5ycd1034(VS.80).aspx

Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TestApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SampleDatabaseDataSet.CustomersRow newCustomersRow = 
                sampleDatabaseDataSet.Customers.NewCustomersRow();

            newCustomersRow.CustomerID  = "AABCD";
            newCustomersRow.CompanyName = "AAA Works";

            sampleDatabaseDataSet.Customers.Rows.Add(newCustomersRow);

            // Why won't these changes commit?
            customersTableAdapter.Update(newCustomersRow);
            sampleDatabaseDataSet.Customers.AcceptChanges();
            customersBindingSource.EndEdit();

            this.customersTableAdapter.Fill(this.sampleDatabaseDataSet.Customers);
        }
    }
}

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.

Rmez
Newbie Poster
2 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
 

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/

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?

ericklan
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You