How do I explicitly get my DataAdapter to insert an edited record as a new one instead of updating it?

Posted on 2006-04-30
Last Modified: 2010-04-23
The Background:

I have a form full of controls bound to a DataTable. This DataTable is one of two in the DataSet, each filled
by their respective DataAdapters and the data source amended accordingly by the DataAdapters Insert/Update/Delete

The Scenario:

I want to build up my data base using my form, but in most cases each record I want to add is slightly different to the last. Instead of re-entering all the data into each control, I want to fill my form with a record (select command), make a slight change and then save it as a new record (insert command).

The Problem:

The myDataAdapter.Update(MyDataSet.MyDataTable1) will overwrite my original record. How do I explicitly insert the amended record as a new record in the data source?

Question by:IvanHowarth
    LVL 142

    Accepted Solution

    Simply assign a SqlCommand that does a INSERT instead of a UPDATE to the dataadapters' UpdateCommand property.
    By default, the dataadapter generates a Update command automatically, which you can however override.
    I am not really good yet in, but know that this is the way to go, hope it helps already
    LVL 6

    Assisted Solution

    LVL 6

    Expert Comment


    i am happy that i could help

    Author Comment


    Your approach is a simple (and cheeky) concept that worked. The only thing that caught me out was one of my two tables I applied this concept to had all the datarows set with the old (so wrong) PK value. A quick loop in changing it to the new value then gave me a new record in the source table.


    Unfortuantely I was nearly there with the above when I saw your more official approach. It nevertheless, I'm sure, would have got me to the same result.

    Thanks both anyway.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now