User
User

Reputation: 13

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

I'm new to Entity Framework, I already searched in google for this problem but I found nothing. I get the error

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

and when I checked the inner exception, I see this

Cannot insert the value NULL into column 'DOCNUM', table 'TCPIDBV2.dbo.tbl_200_Dailyreport'; column does not allow nulls. INSERT fails.

But when I checked the records that I passed in to be saved in the database, there is a value in that object called 'DOCNUM'

This is my code:

[Table("tbl_200_Dailyreport")]
public class tbl_200_Dailyreport
{
    [Key]
    [Required]
    public long DOCNUM {get; set;}
    public DateTime DAILYREPDATE {get; set;}
    public string SECTION {get; set;}
    public string REMARKS { get; set; }
    public int? OPERATIONTIME { get; set; }
    public int? PREPARATIONTIME { get; set; }
    public int? STANDBYTIME { get; set; }
    public int? MATERIALWAITING { get; set; }
    public int? ASSISTTIME { get; set; }
    public int? MAINTENANCETIME { get; set; }
    public int? CLEANINGTIME { get; set; }
    public bool? ISOVERTIME { get; set; }
    public bool? ISLOCKED { get; set; }
    public string SHIFT { get; set; }
    public int? PRODTIME { get; set; }
    public int? BREAKTIME { get; set; }
    public double? TOTALCOLOR { get; set; }
    public double? AVERAGECOLOR { get; set; }
}

In getting a value of those above here is the code:

tbl_200_Dailyreport.DOCNUM = long.Parse(txtReferenceNo.Text);
tbl_200_Dailyreport.PRODTIME = Convert.ToInt32(txtProduction.Text);
tbl_200_Dailyreport.OPERATIONTIME = Convert.ToInt32(txtOperation.Text);
tbl_200_Dailyreport.PREPARATIONTIME = Convert.ToInt32(txtPreparation.Text);
tbl_200_Dailyreport.STANDBYTIME = Convert.ToInt32(txtStandby.Text);
tbl_200_Dailyreport.MAINTENANCETIME = Convert.ToInt32(txtMaintenance.Text);
tbl_200_Dailyreport.CLEANINGTIME = Convert.ToInt32(txtCleaning.Text);
tbl_200_Dailyreport.MATERIALWAITING = Convert.ToInt32(txtMaterial.Text);
tbl_200_Dailyreport.ASSISTTIME = Convert.ToInt32(txtAssist.Text);
tbl_200_Dailyreport.BREAKTIME = Convert.ToInt32(txtOthers.Text);
tbl_200_Dailyreport.DAILYREPDATE = dtpDailyReportDate.Value;
tbl_200_Dailyreport.SHIFT = cboShift.SelectedValue.ToString();
tbl_200_Dailyreport.ISOVERTIME = cbxOvertime.Checked;
tbl_200_Dailyreport.ISLOCKED = false;
tbl_200_Dailyreport.REMARKS = txtRemarks.Text;

if (!String.IsNullOrEmpty(cboSecDepartment.Text)) 
{
    tbl_200_Dailyreport.SECTION = cboSecDepartment.SelectedValue.ToString(); 
}
else
{
    var section = "0";
    tbl_200_Dailyreport.SECTION = section;
}

I allow all of the column to null except the PK called the 'DOCNUM', so in getting the value of 'DOCNUM', where the txtreferenceno.Text has a value that is equal to 1 . and passed it in DailyReportManager so here's the code:

public void Save(tbl_200_Dailyreport records)
{
        try
        {
            this.db.tbl_200_Dailyreport.Add(records);
            this.db.SaveChanges();
        }
        catch (Exception ex)
        {
            throw ex;
        }
}

and in getting the value of txtreferenceno.Text

var max = dailyreportmanager.FindAll().Max(x => (int?)x.DOCNUM) ?? 0;
var addMax = max + 1;
txtReferenceNo.Text = Convert.ToString(addMax);

so for the design of my database

Allow me to post a screenshot:

Database Design

When I checked the records, the 'DOCNUM' has a value equals to one, but why did I still get that error? Please help. A big thanks in advance.

Upvotes: 0

Views: 12520

Answers (1)

mjwills
mjwills

Reputation: 23984

To indicate to Entity Framework that you want to manually specify the key value, specify this attribute:

[DatabaseGenerated(DatabaseGeneratedOption.None)]

against DOCNUM.

Upvotes: 1

Related Questions