Unique Validator: Conversion failed when converting the nvarchar value to data type int (.Net MVC)

Unique Validator: Conversion failed when converting the nvarchar value to data type int (.Net MVC)

aetsfgcuaetsfgcu Posts: 6Questions: 1Answers: 0

I am having an issue when using the Unique Validator on Create

.Field(new Field("Serial")
   .Validator(Validation.Unique(new ValidationOpts
    {
         Message = "An asset with this Serial Number already exists."
     }))
)

When I try to create an item with just a number like 1000. I receive the conversion error:

Conversion failed when converting the nvarchar value 'JXRM1D1' to data type int.

This is the serial number column. But I have another column (Tag) that I use Unique on that i receive:

Conversion failed when converting the nvarchar value '000AT0001252' to data type int.

When I try and create them with a number followed by an letter, 1000z, it works.

I have Serial as well as the Tag set to public string in my view model.

Any help would be appreciated.

Thank you.

Replies

  • allanallan Posts: 61,663Questions: 1Answers: 10,095 Site admin

    Could you try using:

    .Field(new Field("Serial")
       .Validator(Validation.Unique(new ValidationOpts
        {
             Message = "An asset with this Serial Number already exists."
         }, null, null, null, typeof(String)))
    )
    

    That will explicitly set the type to be a string and convert it as such.

    If that doesn't resolve the issue, could you add .TryCatch(false) just after you initialise the Editor instance, and then run the code. If you could let me know what line it breaks on that would be useful. What version of Editor is it that you are using?

    Thanks,
    Allan

  • allanallan Posts: 61,663Questions: 1Answers: 10,095 Site admin

    Sorry - rather than as a parameter the type should be passed in as a generic (the parameter part is an internal parameter - I'll update the documentation to mention the generic). You'd use:

    .Field(new Field("Serial")
       .Validator(Validation.Unique<string>(new ValidationOpts
        {
             Message = "An asset with this Serial Number already exists."
         }))
    )
    

    Allan

  • aetsfgcuaetsfgcu Posts: 6Questions: 1Answers: 0

    Thank you Allan. Passing they type as a generic worked perfectly.

This discussion has been closed.