private void BtnAdd_Click(object sender, EventArgs e)
{
Model.Item items = new Model.Item
{
Code = Convert.ToInt32(txtCode.Text),
Name = txtName.Text,
MQCode = Convert.ToInt32(string.IsNullOrEmpty(txtMinmumQuantityNo.Text) ? null : txtMinmumQuantityNo.Text)
};
db.Items.Add(items);
db.SaveChanges();
}
Error
SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Items_MQ". The conflict occurred in database "C:\USERS\20101\DESKTOP\SIGMA V1\SIGMA V1\BIN\DEBUG\SIGMA.MDF", table "dbo.MQ", column 'MQCode'. The statement has been terminated.
NULL.Convert.ToInt32convertsnullto0.0isn't matching a record in the parent table.