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

  • SEARCH
  • Home
  • 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 8249509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:34:05+00:00 2026-06-07T23:34:05+00:00

I’m having trouble inserting related rows into my database. When I insert new objects

  • 0

I’m having trouble inserting related rows into my database. When I insert new objects into the database in a ASP.NET application, I get a foreign key violation error, whereas in a console application, I do not.

Here are the steps to reproduce the problem:

  1. Create and run the following T-SQL script on a new database:

    create table dbo.[Groups] (
        GroupId int identity primary key,
        Name nvarchar(64) not null
    )
    
    create table dbo.[Objects] (
        ObjectId int identity primary key,
        GroupId int not null foreign key references [Groups] on delete cascade,
        Name nvarchar(64) not null
    )
    
  2. Setup a C# console application named “ConsoleProgram”.

  3. Add a new DataSet named “dsObjects” to the project.
  4. Drag tables “Groups” and “Objects” from the server explorer into the designer.
  5. Edit the relation between the two tables and set the relation to “both relation and foreign key constraint” and update rule to “Cascade”.
  6. Insert the following code into “Main”:

    // (assuming appropriate using statement for dsObjectsTableAdapters)
    var ds = new dsObjects();
    var mgr = new dsObjectsTableAdapters.TableAdapterManager {
        GroupsTableAdapter = new GroupsTableAdapter(),
        ObjectsTableAdapter = new ObjectsTableAdapter()
    };            
    mgr.GroupsTableAdapter.Fill( ds.Groups );
    mgr.ObjectsTableAdapter.Fill( ds.Objects );
    
    var row = ds.Groups.AddGroupsRow( "Fruits" );
    ds.Objects.AddObjectsRow( row, "Apple" );
    ds.Objects.AddObjectsRow( row, "Banana" );
    ds.Objects.AddObjectsRow( row, "Orange" );
    
    mgr.UpdateAll( ds );
    
  7. Run program and verify the new rows were inserted into the respective tables:

    GroupId     Name
    ----------- -------
    1           Fruits
    
    (1 row(s) affected)
    
    ObjectId    GroupId     Name
    ----------- ----------- -------
    1           1           Apple
    2           1           Banana
    3           1           Orange
    
    (3 row(s) affected)
    
  8. Delete the newly created data.

  9. Close the solution.
  10. Create a new ASP.NET Web Application.
  11. Repeat steps 3-5.
  12. Insert the code in step 6 into the Page_Load function of Default.aspx.
  13. Run the program.
  14. Note the exception about the foreign key violation (if any).
  15. Run the console program again and note the data has been created successfully.
  16. Recreate the database and run the ASP.NET application again and note the same exception.

I have tried updating the tables sequentially over using the UpdateAll method, with the same results in both projects.

I am at a complete loss as to why this would work in a ConsoleApplication, but not an ASP.NET application. Any ideas?

  • 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-06-07T23:34:07+00:00Added an answer on June 7, 2026 at 11:34 pm

    Okay, after some searching around, I found the solution to my problem. In the thread, http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/4d10fe87-2de8-42a8-8ef9-b9d46c0fd28d, it was hinted that in order for identity columns to be updated correctly in the dataset, a SELECT statement must be used after the INSERT statement. After exploring the “TableAdapter configuration Wizard”, I found that under the “Advanced Options” there is a “refresh the data table” option which adds the appropriate SELECT statement after INSERTions. For some unknown reason to me, this was unchecked by default in ASP.NET web applications and checked by default in the console application.

    After checking this option, the dataset update works as expected.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am currently running into a problem where an element is coming back from
I have a reasonable size flat file database of text documents mostly saved in

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.