none
Programmatically Inserting Entity Graph To DB RRS feed

  • Question

  • Hi,

    Can somebody please tell me why the code below doesn't work:

    DataSet1 dataSet = new DataSet1();
    OrderTableAdapter ota = new OrderTableAdapter();
    CustomerTableAdapter cta = new CustomerTableAdapter();
    DataSet1.CustomerRow customerRow = dataSet.Customer.NewCustomerRow();
    customerRow.Name = "Customer #1";
    dataSet.Customer.AddCustomerRow(customerRow);
    DataSet1.OrderRow orderRow = dataSet.Order.NewOrderRow();
    orderRow.CustomerRow = customerRow;
    orderRow.Date = new DateTime(1990, 1, 1);
    orderRow.Comment = "There is no comment.";
    dataSet.Order.AddOrderRow(orderRow);
    dataSet.AcceptChanges();
    cta.Update(dataSet.Customer);
    ota.Update(dataSet.Order);
    
    No exception occurs, but the data don't appear in the db.

    Thanks.
    Sunday, October 4, 2009 7:37 PM

Answers

  • Remove "dataSet.AcceptChanges();" line of code before calling Update methods and it should work fine

    Val Mazur (MVP) http://www.xporttools.net
    Monday, October 5, 2009 10:32 AM
    Moderator
  • Okay, fortunately I found the solution in this topic:
    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4d10fe87-2de8-42a8-8ef9-b9d46c0fd28d

    I had to set the update rule to cascade in the data relation in my dataset.
    • Marked as answer by vmware Monday, October 5, 2009 11:13 AM
    Monday, October 5, 2009 11:13 AM

All replies

  • Is something wrong with my question? :-)

    I thought it was going to be an-easy-to-answer question.

    Maybe one code row is missing or I am forgetting to set a flag.

    The question itself is targeting the base functionality of DataSets.
    Monday, October 5, 2009 7:58 AM
  • I forgot the mention that the DataSet was mapped to these two tables:

    Customer
        - ID: integer, auto-increment, primary key
        - Name: nvarchar(50)
    Order
        - ID: integer, auto-increment, primary key
        - Customer: integer, foreign key
        - Date: datetime
        - Comment: nvarchar(50)
    Monday, October 5, 2009 9:06 AM
  • Remove "dataSet.AcceptChanges();" line of code before calling Update methods and it should work fine

    Val Mazur (MVP) http://www.xporttools.net
    Monday, October 5, 2009 10:32 AM
    Moderator
  • I removed it.

    Then I get an exception from the database engine saying that a foreign key constraint is voilated.
    Monday, October 5, 2009 10:39 AM
  • The only thing I could think of as a possible reason is that my insert-customer statement is incorrect.

    But I don't think it is incorrect. It's auto-generated by VS designer, and it does return back the last inserted ID.

    INSERT INTO [dbo].[Customer] ([Name]) VALUES (@Name);
    SELECT ID, Name FROM Customer WHERE (ID = SCOPE_IDENTITY())
    Monday, October 5, 2009 10:41 AM
  • Sometimes developers forget to set the identity specification for foreign keys, which could have been a problem, but I didn't forget it.
    Monday, October 5, 2009 10:46 AM
  • I have also tried to do the insert process by using a TableAdapterManager this way (without success):

    DataSet1 dataSet = new DataSet1();
    OrderTableAdapter ota = new OrderTableAdapter();
    CustomerTableAdapter cta = new CustomerTableAdapter();
    DataSet1.CustomerRow customerRow = dataSet.Customer.NewCustomerRow();
    customerRow.Name = "Customer #1";
    dataSet.Customer.AddCustomerRow(customerRow);
    DataSet1.OrderRow orderRow = dataSet.Order.NewOrderRow();
    orderRow.CustomerRow = customerRow;
    orderRow.Date = new DateTime(1990, 1, 1);
    orderRow.Comment = "There is no comment.";
    dataSet.Order.AddOrderRow(orderRow);

    TableAdapterManager tam = new TableAdapterManager();
    tam.CustomerTableAdapter = cta;
    tam.OrderTableAdapter = ota;
    tam.UpdateAll(dataSet);
    Monday, October 5, 2009 10:58 AM
  • Okay, fortunately I found the solution in this topic:
    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4d10fe87-2de8-42a8-8ef9-b9d46c0fd28d

    I had to set the update rule to cascade in the data relation in my dataset.
    • Marked as answer by vmware Monday, October 5, 2009 11:13 AM
    Monday, October 5, 2009 11:13 AM