Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am facing an issue in deleting record from a table. I have got two table as [BusRoute] and [SchoolBusRoute] and the respective fields in both the tables are

[BusRoute]

1) BusRouteID
2) RouteNo
3) TflUrl
4) DetailsofRoute
5) Active
6) DateChanged
7) ChangedByID

[SchoolBusRoute]

1) SchoolBusRouteID
2) SchoolID
3) BusRouteID
4) DateChanged
5) ChangedByID

Where the field BusRouteID in the table [SchoolBusRoute] id dependent on the field BusRouteID in the table [BusRoute]. The particular field is a primary key in the table [BusRoute] and so there will be no duplicate records. But we have got duplicate records of the field [BusRouteID] for different schoolID in the table [SchoolBusRoute]. Hence because of this when I try to delete a record in the table [BusRoute] it is conflicting with the table [SchoolBusRoute] although I have deleted the corresponding record against the particular school in the table [SchoolBusRoute] as it still have the same BusRouteID against other schoolID.


When I try to perform the same operation using C# coding in visual studio and still I am unable to delete as it displays an exception as 'Cannot insert the value NULL into column 'BusRouteID', table 'TflStars.dbo.SchoolBusRoute'; column does not null allow nulls. UPDATE fails'

The code I used to delete the record from both the table is
C#
else if (e.CommandName == "DeleteRecord")
{
    //Delete and raise event to rebibnd
    SchoolBusRoute thisSchoolBusRoute = selectedBusRoute.SchoolBusRoutes.Where(p => p.BusRouteID == selectedBusRoute.BusRouteID).FirstOrDefault();

    if (thisSchoolBusRoute != null)
    {
        schoolBusRoutesService.Delete(thisSchoolBusRoute);
        BusRoutesService.Delete(selectedBusRoute);

        if (OnBusRouteStatusChanged != null)
        {
            EventArgs newArgs = new EventArgs();
            OnBusRouteStatusChanged(this, newArgs);
        }
    }
}

public void Delete(SchoolBusRoute DataEntity)
{
    SchoolBusRoute entityInstance = currentContext.SchoolBusRoutes.Where(p => p.SchoolBusRouteID == DataEntity.SchoolBusRouteID).FirstOrDefault();

    if (entityInstance != null)
    {
        currentContext.DeleteObject(entityInstance);
        currentContext.SaveChanges();
    }
}

public void Delete(BusRoute DataEntity)
{
    BusRoute entityInstance = currentContext.BusRoutes.Where(p => p.BusRouteID == DataEntity.BusRouteID).FirstOrDefault();

	if (entityInstance != null)
    {
        currentContext.DeleteObject(entityInstance);
        currentContext.SaveChanges();
    }
}

I am getting the exception in the statement

currentContext.SaveChanges();
that is in the function 'Public void Delete(BusRoute DataEntity)'

Can anyone help me to provide a solution to delete a record in the table [BusRoute] without conflicting with the table [SchoolBusRoute].
Posted
Updated 7-Jan-16 0:06am
v3
Comments
Suvendu Shekhar Giri 7-Jan-16 5:44am    
Share the SQL Statement you are using.
Abhinav S 7-Jan-16 5:47am    
I'm a little confused. You are not deleting a record but rather updating right?
[no name] 7-Jan-16 5:50am    
could you please share your code here
Tomas Takac 7-Jan-16 5:56am    
Do not post code in comments. Use Improve question to update your question.
user 3008 7-Jan-16 6:03am    
Have updated the question with the code now. Thanks for your suggestion.

1 solution

You have three options:

  1. You only allow deleting a BusRoute when it's not used by any SchoolBusRoutes.
  2. You always delete all SchoolBusRoutes that are using that BusRoute.
  3. You disconnect the SchoolBusRoute for the BusRoute by setting it to another BusRouteId or NULL.

Otherwise the foreign key will nto allow you to delete the BusRoute.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900