none
Add new row to typed DataSet RRS feed

  • Question

  • I have a typed DataSet. There's a table with an auto-incrementing integer primary key column.

    After I call NewRow, I get a new row with correct schema and default values (DBNull). I assign some dummy values and add to the table.

    It's working fine except the primary key column. It doesn't allow null, and I don't know the next available integer, so I give -1. Then I add the row to the table. 

    In database, I find that the primary key column gets assigned the next available integer automatically, but the cached version in memory still has the value of -1 until next time I run the project.

    I wonder how this contradiction is handled usually. Refill the table after save?

    Monday, October 5, 2009 6:53 PM

Answers

  • Yes, you need to add functionality to populate DataSet with the generated IDs.
    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Wednesday, October 7, 2009 10:18 AM
    Moderator
  • You could always put that functionality in a partial class of your Typed DataSet ... that way it won't interfere with re-generation of your schema.
    ~~Bonnie Berent [C# MVP]

    (new blog --- not many posts yet --- be patient)
    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Sunday, October 11, 2009 12:13 AM
  • Hmmm ... in your original post, I thought that you said you set the id column to -1. But, I believe that you should be able to change the ReadOnly property to false in your partial class. I've experimented a bit, and this is what I came up with ... it should work ok for you:

    public partial class MyDataSet
    {
        public partial class MyParentDataTable
        {
            public override void EndInit()
            {
                this.columnMyPrimaryKey.ReadOnly = false;
                base.EndInit();
            }
        }
    }

    ~~Bonnie Berent [C# MVP]

    (new blog --- not many posts yet --- be patient)
    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Sunday, October 11, 2009 4:32 PM

All replies

  • You need to retrieve generated identity values from your database and update those values in DataSet. Here are some samples how to work with identity columns in .NET

    http://bytes.com/topic/asp-net/answers/323505-retrieving-identity-after-dataset-update
    Val Mazur (MVP) http://www.xporttools.net
    Tuesday, October 6, 2009 10:39 AM
    Moderator
  • So I need to change the id columns to be writable on the client side schema manually? I believe this solution is hard to maintain when I re-generate a client-side schema from database.
    Tuesday, October 6, 2009 3:07 PM
  • Yes, you need to add functionality to populate DataSet with the generated IDs.
    Val Mazur (MVP) http://www.xporttools.net
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Wednesday, October 7, 2009 10:18 AM
    Moderator
  • You could always put that functionality in a partial class of your Typed DataSet ... that way it won't interfere with re-generation of your schema.
    ~~Bonnie Berent [C# MVP]

    (new blog --- not many posts yet --- be patient)
    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Sunday, October 11, 2009 12:13 AM
  • problem is the id columns are read only. is it possible to override the read only property in my written partial class?
    Sunday, October 11, 2009 1:36 AM
  • Hmmm ... in your original post, I thought that you said you set the id column to -1. But, I believe that you should be able to change the ReadOnly property to false in your partial class. I've experimented a bit, and this is what I came up with ... it should work ok for you:

    public partial class MyDataSet
    {
        public partial class MyParentDataTable
        {
            public override void EndInit()
            {
                this.columnMyPrimaryKey.ReadOnly = false;
                base.EndInit();
            }
        }
    }

    ~~Bonnie Berent [C# MVP]

    (new blog --- not many posts yet --- be patient)
    geek-goddess-bonnie.blogspot.com
    • Marked as answer by Yichun_Feng Monday, October 12, 2009 1:07 AM
    Sunday, October 11, 2009 4:32 PM