Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio 2015 & .NET 4.6
  4. Getting the Error Description from ModelState

Getting the Error Description from ModelState

Scheduled Pinned Locked Moved Visual Studio 2015 & .NET 4.6
helpdatabasequestionannouncement
2 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ADSCNET
    wrote on last edited by
    #1

    Hi, I'm using a FormView and update method which try to update the database, but it's throwing and error, I need to know on which field the error was but I am not able to extract the error msg from the ModelStateDictionary so I can know what is the exact error msg, the code as follow:

    Public Function UpdateTest(id As Integer) As Integer
    Dim Employee = _db.DbSet_Employees.Find(id)

        Try
    
            If Employee Is Nothing Then
                ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be found", id))
                Return 0
            End If
    
            TryUpdateModel(Employee)
            If Not ModelState.IsValid Then
                ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be updated", id))
    
                MsgBox(ModelState.GetErrorMessageForKey("error"))
    
                Return 0
            End If
    
        Catch ex As Exception
            ' Me.lblMsg.Text = Err.Description.ToString
            Me.lblMsg2.Text = ex.Message.ToString
            Return 0
        End Try
    
        ' write the changes to the database
        Return \_db.SaveChanges
    End Function
    

    Public Module ExtMethod
    <System.Runtime.CompilerServices.Extension> _
    Public Function GetErrorMessageForKey(ByVal dictionary As ModelStateDictionary, ByVal key As String) As String
    Return dictionary(key).Errors.First().ErrorMessage
    End Function
    End Module

    The problem start from TryUpdateModel then in the temperrory line MsgBox() Thanks for the help.

    A 1 Reply Last reply
    0
    • A ADSCNET

      Hi, I'm using a FormView and update method which try to update the database, but it's throwing and error, I need to know on which field the error was but I am not able to extract the error msg from the ModelStateDictionary so I can know what is the exact error msg, the code as follow:

      Public Function UpdateTest(id As Integer) As Integer
      Dim Employee = _db.DbSet_Employees.Find(id)

          Try
      
              If Employee Is Nothing Then
                  ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be found", id))
                  Return 0
              End If
      
              TryUpdateModel(Employee)
              If Not ModelState.IsValid Then
                  ModelState.AddModelError("ModelError", String.Format("Employee with ID {0} could not be updated", id))
      
                  MsgBox(ModelState.GetErrorMessageForKey("error"))
      
                  Return 0
              End If
      
          Catch ex As Exception
              ' Me.lblMsg.Text = Err.Description.ToString
              Me.lblMsg2.Text = ex.Message.ToString
              Return 0
          End Try
      
          ' write the changes to the database
          Return \_db.SaveChanges
      End Function
      

      Public Module ExtMethod
      <System.Runtime.CompilerServices.Extension> _
      Public Function GetErrorMessageForKey(ByVal dictionary As ModelStateDictionary, ByVal key As String) As String
      Return dictionary(key).Errors.First().ErrorMessage
      End Function
      End Module

      The problem start from TryUpdateModel then in the temperrory line MsgBox() Thanks for the help.

      A Offline
      A Offline
      ADSCNET
      wrote on last edited by
      #2

      I've done the following, created a model & then a function to be able to use it around my app:

      Public Function GetErrorList(ByVal modelState As ModelStateDictionary) As String
      Dim errorList = ( _
      From item In modelState _
      Where item.Value.Errors.Any() _
      Select item.Value.Errors(0).ErrorMessage).ToList()

          Return "Error in Rec. {0}: " & errorList.Item(0).ToString
      

      End Function

      Then I call the function:

      TryUpdateModel(Customer)
      If Not ModelState.IsValid Then
      ModelState.AddModelError("ModelError", String.Format(GetErrorList(ModelState), id))
      Return 0
      End If

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups