none
Solution: A C# answer for issue Q313540 relating to TableAdapter and AutoIncrement field. RRS feed

  • General discussion

  •  

    Dear all,

     

    Q313540 is: "The Merge method may create duplicate records when you try to update records with an AutoIncrement field".

     

    Here is my answer to this problem in C#.

     

    1)

    At a DataSet level, use the IDE to generate a "Partial Class" file.

    Add the following public and static method.

    (My DataSet is called AppData)

     

    Code Block

    namespace MyNameSpace

    {

    public partial class AppData

    {

    public static void TableAdapter_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)

    {

    if (e.StatementType == System.Data.StatementType.Insert)

    e.Status = System.Data.UpdateStatus.SkipCurrentRow;

    }

    }

    }

     

    2)

    On the same page (just to keep everything at the same place!), add the following code:

     

    Code Block

    //Watch the namespace to match the designer's!!!

    namespace MyNameSpace.MyNameSpaceTableAdapters

    {

    public partial class MyTableAdapter

    {

    // I create a new constructor with a parameter only to make a difference with the constructor created by the designer

    public MyTableAdapter(bool rowUpdatedHandling)

    {

    // ClearBeforeFill is there because it's in the original constructor...

    this.ClearBeforeFill = true;

    this.Adapter.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(AppData.TableAdapter_RowUpdated);

    }

    }

    }

     

     

    3)

    When you call your Table Adapter, use the overloaded constructor if needed.

     

    Code Block
    MyTableAdapter tableAdapter = new MyTableAdapter (true);

     

     

    Stephane EHRET,

    City of Bristol College,

    England

    Thursday, October 25, 2007 9:54 AM