locked
Unhandled Exception RRS feed

  • Question

  • User1421620300 posted

    I have error in my project that says its user exception but it does not seem as any error in project.  SSMS is connection is fine too! Everything seems okay in user code also. Is their an explanation? Markus

    https://imgur.com/a/irPYb8l

    Monday, October 22, 2018 5:08 PM

Answers

  • User1120430333 posted

    DBUpdateException is thrown when the state of the entity is not set so that EF does an insert or update of the entity persisting it to the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 7:29 PM
  • User2053451246 posted

    You most likely aren't providing a value required by the database.  Add this code in a catch block to have it log the errors in the inner exception to the output window in VS:

    https://stackoverflow.com/a/7798264

    Not the mention of using Debug.Write instead of Console.Write since you are debugging a web application.  Make sure to check out the edit that says "A few slight improvements" and add that code to get the value of the field.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 7:58 PM
  • User1520731567 posted

    Hi Markus33,

    According to your picture,

    Looks like you got a problem with your data model or the data you want to persist.

    I guess some required field or foreign key is not set when you try to save the entity.

    For example:

    Id column marked as Identity column in the database. You can not set value for such column explicitly.

    You are setting value of Id property. That is why you are seeing that exception.

    SQL server will assigned value to the IDENTITY column while inserting the record to the table.

    So you have following solutions:

    1.Remove IDENTITY constraint from the column in the database. This way your current code will work fine.

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    2.You don't assigned values to the Id property(check your model in httppost action,Id=0)

    3.You assign the value to Id property but you need to set the indentity_insert ON on the table before inserting the rows with explicit IDs and then set if OFF after the insertion.

    db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Customer OFF")

    4.Try catching and logging the System.Data.Entity.Infrastructure.DbUpdateException.

    It should tell you which entity causes the problem. Here is some logging code that we use:

    using NLog;
    
    private static Logger Logger = LogManager.GetCurrentClassLogger();
    
    try {
        _db.SaveChanges();
    }
    catch (System.Data.Entity.Infrastructure.DbUpdateException upEx) {
    
        if (upEx.Entries != null && upEx.Entries.Any()) {
            Logger.Debug("DbUpdateException contained '{0}' entries:", upEx.Entries.Count());
    
             // get info about the Entity that produced the error
             foreach (var dbEntityEntry in upEx.Entries) {
                 if (dbEntityEntry.Entity != null) {
                     var entityType = dbEntityEntry.Entity.GetType();
                     try {
                         var id = entityType.GetProperty("Id").GetValue(dbEntityEntry.Entity, null);
                         Logger.Debug("DbUpdateException contains DbEntityEntry - Type: '{0}', Id: '{1}', State: '{2}'", entityType.Name, id, dbEntityEntry.State.ToString("G"));
                     } catch (Exception) {
                         Logger.Debug("DbUpdateException contains DbEntityEntry - Type '{0}', Id: unknown, State: '{2}'", entityType.Name, dbEntityEntry.State.ToString("G"));
                     }
                 }
             }
        } 
    }

    This link is similar to your issue,you could refer to:

    https://stackoverflow.com/questions/42560165/there-is-a-dbupdateexception-was-unhandled-by-user-code-error-when-running-cod

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2018 6:48 AM
  • User1120430333 posted

    The screen image an the Exception messagebox is telling you to look at the inner.exception.message about the exception. You can see the inner.exception.message if you click on View Detail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 24, 2018 10:09 AM

All replies

  • User1120430333 posted

    DBUpdateException is thrown when the state of the entity is not set so that EF does an insert or update of the entity persisting it to the database.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 7:29 PM
  • User2053451246 posted

    You most likely aren't providing a value required by the database.  Add this code in a catch block to have it log the errors in the inner exception to the output window in VS:

    https://stackoverflow.com/a/7798264

    Not the mention of using Debug.Write instead of Console.Write since you are debugging a web application.  Make sure to check out the edit that says "A few slight improvements" and add that code to get the value of the field.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 22, 2018 7:58 PM
  • User1520731567 posted

    Hi Markus33,

    According to your picture,

    Looks like you got a problem with your data model or the data you want to persist.

    I guess some required field or foreign key is not set when you try to save the entity.

    For example:

    Id column marked as Identity column in the database. You can not set value for such column explicitly.

    You are setting value of Id property. That is why you are seeing that exception.

    SQL server will assigned value to the IDENTITY column while inserting the record to the table.

    So you have following solutions:

    1.Remove IDENTITY constraint from the column in the database. This way your current code will work fine.

    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    2.You don't assigned values to the Id property(check your model in httppost action,Id=0)

    3.You assign the value to Id property but you need to set the indentity_insert ON on the table before inserting the rows with explicit IDs and then set if OFF after the insertion.

    db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Customer OFF")

    4.Try catching and logging the System.Data.Entity.Infrastructure.DbUpdateException.

    It should tell you which entity causes the problem. Here is some logging code that we use:

    using NLog;
    
    private static Logger Logger = LogManager.GetCurrentClassLogger();
    
    try {
        _db.SaveChanges();
    }
    catch (System.Data.Entity.Infrastructure.DbUpdateException upEx) {
    
        if (upEx.Entries != null && upEx.Entries.Any()) {
            Logger.Debug("DbUpdateException contained '{0}' entries:", upEx.Entries.Count());
    
             // get info about the Entity that produced the error
             foreach (var dbEntityEntry in upEx.Entries) {
                 if (dbEntityEntry.Entity != null) {
                     var entityType = dbEntityEntry.Entity.GetType();
                     try {
                         var id = entityType.GetProperty("Id").GetValue(dbEntityEntry.Entity, null);
                         Logger.Debug("DbUpdateException contains DbEntityEntry - Type: '{0}', Id: '{1}', State: '{2}'", entityType.Name, id, dbEntityEntry.State.ToString("G"));
                     } catch (Exception) {
                         Logger.Debug("DbUpdateException contains DbEntityEntry - Type '{0}', Id: unknown, State: '{2}'", entityType.Name, dbEntityEntry.State.ToString("G"));
                     }
                 }
             }
        } 
    }

    This link is similar to your issue,you could refer to:

    https://stackoverflow.com/questions/42560165/there-is-a-dbupdateexception-was-unhandled-by-user-code-error-when-running-cod

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2018 6:48 AM
  • User1120430333 posted

    The screen image an the Exception messagebox is telling you to look at the inner.exception.message about the exception. You can see the inner.exception.message if you click on View Detail

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 24, 2018 10:09 AM