When you get validation error on DB, but.. don’t know what.

One day soon, my blog is going to be filled with lists of errors and what to do about them.

When you are working with data, (and who doesn’t work with data today..)  you may encounter validation error.  The error is straight forward, you are trying to insert invalid data.

The problem is that invalid error is not easy to see, specially when you have multiple fields.

You can write catch code like below and find some more insights.


Input Validation from Model

If you want to improve user experience of your web services, you need to work on easy and friendly input interfaces for users through out.  Input validation being one of them, and validating user input for ASP.Net MVC is simple and intuitive.  You can use below at Model.

  • Required()
    • This makes specific field a required field.
  • MaxLength(max)
    • This specifies max length users can input for the field.
  • MinLength(min)
    • This specifies min length users can input for the field.
  • Range(min, max)
    • This specifies range of length (from min to max) users can input for the field.
  • RegularExpression(pattern)
    • This specified regular expression that users must match for the field.
  • EmailAddress()
    • The field must be email address. you can do same with RegulareExpression(pattern)
  • Url()
    • The field must be url.
  • Compare(other)
    • The field must match other.
  • CustomValidation(type,method)
    • This calls custom validation.

*The Model must make reference to System.ComponentModel and System.ComponentModel.DataAnnotations.

You can refer to http://www.asp.net/mvc/overview/getting-started/introduction/adding-validation too.