Click here to Skip to main content
15,883,940 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have few list of contact in my web page where I have "edit" and "delete" buttons besides each contact. The edit functionality is working perfectly but I am getting an exception while performing the delete operation. I get the following exception

"An error occurred while updating the entries. See the inner exception for details."

and the inner exception says as

"Cannot insert the value NULL into column 'ContactID', table 'TflStars.dbo.SchoolContact'; column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated."

The above exception in the statement

C#
currentContext.SaveChanges();


I even checked the database in the column "ContactID". The column is assigned with primary key and the "ALlowNUll" is not been checked and cannot be checked since it is a primary key field. I cannot also delete the primary key because it is been refer ed by another another table through foreign key.

Can anyone please help me resolve this issue

AL EDIT: code from comment:
This is the code that performs the delete

C#
LinkButton btnDelete = (LinkButton)e.Item.FindControl("btnDelete");

if (btnDelete != null)
{
 if (this.ReadOnly)
 {
   btnDelete.Visible = false;
 }
 else
 {
  btnDelete.Visible = true;

  if (thisContact.UserDetails != null && thisContact.UserDetails.Count > 0)
  {
   btnDelete.CommandName = "CannotDelete";
   btnDelete.OnClientClick = "return window.alert('This contact cannot be deleted as there is a user login record using it')";
  }
  else
  {
   btnDelete.OnClientClick = "return window.confirm('Are you sure you want to delete this staff contact?')";
  }
  btnDelete.CommandArgument = thisContact.ContactID.ToString();

  if (thisContact.ContactID == SessionContents.CurrentUser.ContactID)
  {
   btnDelete.Visible = false;
  }
 }
}
Posted
Updated 30-Apr-15 5:12am
v2
Comments
ZurdoDev 30-Apr-15 10:01am    
The error means that your code is sending NULL for that column. You need to fix that.
virusstorm 30-Apr-15 10:14am    
Can you post the code that is performing the delete. I suspect the issue might be a cascade problem.
user 3008 30-Apr-15 10:59am    
LinkButton btnDelete = (LinkButton)e.Item.FindControl("btnDelete");

if (btnDelete != null)
{
if (this.ReadOnly)
{
btnDelete.Visible = false;
}
else
{
btnDelete.Visible = true;

if (thisContact.UserDetails != null && thisContact.UserDetails.Count > 0)
{
btnDelete.CommandName = "CannotDelete";
btnDelete.OnClientClick = "return window.alert('This contact cannot be deleted as there is a user login record using it')";
}
else
{
btnDelete.OnClientClick = "return window.confirm('Are you sure you want to delete this staff contact?')";
}
btnDelete.CommandArgument = thisContact.ContactID.ToString();

if (thisContact.ContactID == SessionContents.CurrentUser.ContactID)
{
btnDelete.Visible = false;
}
}
}
virusstorm 30-Apr-15 11:36am    
I don't see the code where you are trying to delete the entity.
Joan Magnet 30-Apr-15 10:15am    
Are you using a typed dataset?

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