Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:53:52+00:00 2026-05-23T13:53:52+00:00

NOTE: This is the simple version of my previous question that SHOULD have been

  • 0

NOTE: This is the simple version of my previous question that SHOULD have been written.

I have two tables in the same SQL Server database, TestTable and TestTable2. Neither has a primary key. Each have two columns,

  1. TestInt (int)
  2. TestString (varchar(50))

There is data in TestTable2, while TestTable is empty. I want to copy the contents of one into the other, via C#. (I know this can be done in SQL, but humor me here.)

Here is the code I’ve written to do the job, using a SqlDataAdapter object…

// Data in TestTable2, nothing in TestTable.
private static void Main(string[] args)
{
    SqlConnection conn = /* CONNECTION CREATION CODE */
    string sqlQuery = "SELECT * FROM TestTable2";
    DataTable payload = /* SIMPLE QUERY CODE USING sqlQuery */

    // CODE CHECKPOINT #1

    sqlQuery = "SELECT * FROM TestTable";
    SqlCommand sCmd = new SqlCommand(sqlQuery, conn);
    SqlDataAdapter sDA = new SqlDataAdapter(sCmd);     
    DataSet dataSet = new DataSet();     
    conn.Open();     
    sDA.Fill(dataSet);     
    conn.Close();

    for (int i = 0; i < payload.Rows.Count; i++)
    {
        dataSet.Tables[0].ImportRow(payload.Rows[i]);
    }

    // CODE CHECKPOINT #2

    SqlCommandBuilder cb = new SqlCommandBuilder(sDA); 

    conn.Open();     
    sDA.Update(dataSet);     
    conn.Close();
}

The problem is that this code is not working. If I were to check the contents of DataTable ‘payload’ at Checkpoint #1, I see 4 rows, while there are 0 rows in dataSet.Tables[0]. If I then check the contents of ‘dataSet’ at Checkpoint #2, I see 4 rows in dataSet.Tables[0]! However, at the end of the program, none of the rows from TestTable2 have made it into TestTable.

In other words, C# is moving the data between the DataTables, but it is having no affect on the tables themselves.

I’ve found that adding newly created rows

DataRow row = DataSet.DataTable.NewRow(...);
...
dataSet.Tables[i].Rows.Add(row);

into the destination DataSet works with SqlDataAdapter.Update, but that’s not appropriate in this case, as you cannot use DataTable.Rows.Add on a row held by a separate DataTable. Hence, my problem, as this has rendered me incapable of transfering data from one table to another, especially in cases where large numbers of column are involved, rendering explicit SQL commands very clunky.

Is there something I’m missing in this code that I’m not seeing? If so, what is it?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:53:53+00:00Added an answer on May 23, 2026 at 1:53 pm

    Try DataSet.AcceptChanges() before updating data to the DB. Also, check, what the DataSet.GetChanges() method returns. It should not be an empty DataSet.

    Finally, I have found the solution here:

    importrow –> update fails

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a simple stored procedure that looks like this (note: this
Note: this appears to have been fixed in Roslyn This question arose when writing
(Note: This is for MySQL's SQL, not SQL Server.) I have a database column
Note that this function does not have a { and } body. Just a
Summary: I have a struct that is read/written to file. This struct changes frequently,
I have been dealing with this for at least two days now. I don't
Note: This is not a question whether I should use list or deque. It's
Note This is not a REBOL-specific question. You can answer it in any language.
(NOTE: This question is not about escaping queries, it's about escaping results) I'm using
Note: This is the opposite direction to most similar questions! I have an iPhone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.