Editor C# API gives an exception of Data set for 'XXX' is not of type Nullable`1

Editor C# API gives an exception of Data set for 'XXX' is not of type Nullable`1

maorlastmaorlast Posts: 5Questions: 2Answers: 0

I have a Product Class that has some fields that are Nullable (also in SQL Database)

public class Product
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public decimal? DesiredPrice { get; set; }
    }

Once I initialize the Editor plugin and inline update a cell I got the following Exception:
Editor C# API gives an exception of Data set for 'DesiredPrice' is not of type Nullable`1

public class ActiveController : ApiController
    {
        [Route("Home/api/Active/GetActiveListings")]
        [HttpPost, HttpGet]
        public IHttpActionResult GetActiveListings()
        {
            try
            {
                var request = HttpContext.Current.Request;
                var settings = Properties.Settings.Default;
                using (var db = new Database(settings.DbType, settings.ConnectionString))
                {
                    var response = new Editor(db, "Products", "Id")
                        .Model<Product>()
                        .Process(request)
                        .Data();

                    return Json(response);
                }
            }
            catch (Exception ex)
            {
                // Log this
                return BadRequest("error was found~!!");
            }
        }

How can I overcome this issue? It is vital for me that some cols in the database table will be Nullable

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    edited June 2015

    Hi,

    So the solution here is slightly nonintuitive in C# - the main problem is that null cannot be represented using the HTTP parameters that Editor submits to the server (an empty value can be null or an empty string). So what we need to do is use a set formatter to convert the empty string submitted into a null for the database. This is done using the NullEmpty() formatter:

    .Field(new Field("DesiredPrice")
        .SetFormatter(Format.NullEmpty())
    )
    

    Also remove the ? nullable type flag from the DesiredPrice parameter.

    Regards,
    Allan

  • maorlastmaorlast Posts: 5Questions: 2Answers: 0

    Where exactly in the code I should put it?

    thanks

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    edited June 2015

    Immediately after the .Model<Product>() line.

    The Field method is a method of the Editor class and basically provides additional information for Editor, since the model can't fully convey everything that is needed.

    Allan

  • maorlastmaorlast Posts: 5Questions: 2Answers: 0

    Okay, I put it. when I try to edit via the Editor and give it the value of 44 I get "Cannot convert type 'int' to 'string'"

    When I put the value of 4.3 I get "Cannot convert type 'decimal' to 'string'"

    When I put the value of Foo I get "Error converting data type nvarchar to numeric."

    How can I convert it to Decimal? how does the conversion work?

    Thanks

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    Answer ✓

    Hi,

    Sorry for the delay in replying. This sounds like a type conversion issue in Editor 1.4.2 unfortunately.

    If you set the type to string in the model it should work as expected and the type will still be correct in the database. The values are passed around internally as dynamic or object so there needs to be some type casting happening in the dll.

    I've just tried to reproduce the issue with Editor 1.5 which is in development and should be released in the next few weeks and it works as expected.

    Regards,
    Allan

This discussion has been closed.