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 7401313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:25:59+00:00 2026-05-29T04:25:59+00:00

I have an asp.net application with a c# code-behind, connected to an SQL db

  • 0

I have an asp.net application with a c# code-behind, connected to an SQL db with linq-to-entities… When I attempt to ‘SaveChanges()’ on the following code I get an exception (listed below). Any thoughts on what is up?

private void setNewRide(long newRideID, int carNum)
        {
            handleCompletedRide(carNum);

            using (myEntities = new RamRideOpsEntities())
            {
                Vehicle assignedCar = myEntities.Vehicles.FirstOrDefault(car => car.CarNum == carNum);
                Ride newRide = myEntities.Rides.FirstOrDefault(ride => ride.identity == newRideID);

                if (assignedCar != null && newRide != null)
                {
                    vs_CurrentRideId = newRide.identity; //Save current ride to ViewState
                    vs_CarStatus = assignedCar.Status; //Save old status to ViewState

                    assignedCar.Status = "EnRoute";
                    assignedCar.CurrPassengers = newRide.NumPatrons;
                    assignedCar.StartAdd = newRide.PickupAddress;
                    assignedCar.EndAdd = newRide.DropoffAddress;
                    assignedCar.CurrentAdd = newRide.DropoffAddress;

                    assignedCar.Rides.Add(newRide);

                    newRide.TimeDispatched = DateTime.Now;
                    newRide.WaitTime = (((DateTime)newRide.TimeDispatched) - ((DateTime)newRide.TimeOfCall));
                    newRide.AssignedCar = carNum;
                    newRide.Status = "EnRoute";

                    myEntities.SaveChanges(); //EXCEPTION HERE!
                    SelectCarUP.DataBind();
                    SelectCarUP.Update();                    
                }
            }
        }

THE EXCEPTION:

The UPDATE statement conflicted with the FOREIGN KEY constraint
\”FK_Rides_Vehicles\”. The conflict occurred in database
\”CWIS29RamRideOps\”, table \”dbo.Vehicles\”, column
‘Identity’.\r\nThe statement has been terminated.

THE DB:

enter image description here

  • 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-05-29T04:26:00+00:00Added an answer on May 29, 2026 at 4:26 am

    This line:

    assignedCar.Rides.Add(newRide);
    

    is translated as SQL-INSERT – while you already have a record with the same ID. Decide what you want to do: insert a new ride (in which case you should NULLify the id of newRide), or update it (in which case you should just comment that line out; changes will be saved).

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

Sidebar

Related Questions

I have an asp.net application with c# code-behind. This application is a dispatch system
I have this code in my ASP.NET application written in C# that is trying
I have a code base that has been used as an ASP.Net web application.
I have an ASP .NET application that connects to an Oracle or a SQL
I have created an ASP.NET Webforms web application which basically inetracts with the code
Why are the code-behind pages for an ASP.NET web application locked at run time?
I've inherited an ecommerce ASP.NET (c# code behind) web application. We've recently moved servers
I have an asp.net application in which i want to get the value of
Suppose I have an ASP.NET application running across several web servers behind a load
I have an Asp.NET application (VS2008, Framework 2.0). When I try to set a

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.