I'm new to C# programming. I've written a program in C# which has been connected to a SQL Server database through LINQ connector. Everything ran smoothly. But now I get below error when I want to delete a record from program with a specific condition:
An unhandled exception of type 'System.ArgumentNullException' occurred in EntityFramework.dll
Additional information: Value cannot be null.
This is my C# code:
private void btnDel_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Sales system ", "Are you sure to delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
string photo = txtPhoto.ToString();
var edit = db.tblPersons.Where(c => c.PhotoNo == photo || c.ReceiptNo == photo).FirstOrDefault();
db.tblPersons.Remove(edit);
db.SaveChanges();
MessageBox.Show("Info deleted");
}
}
Does anyone know about my issue in this occasion?
txtPhotofield is getting populated. It looks like maybephotois an invalid value, which is causingeditto be null, and entity framework cannot remove anullvalue fromtblPersons.editis null, obviously. Find out why + check for null.