object-reference-not-set-to-an-instance-of-an-object in Editor version 2.0.4 but not 1.9.6

object-reference-not-set-to-an-instance-of-an-object in Editor version 2.0.4 but not 1.9.6

rlb5128rlb5128 Posts: 9Questions: 2Answers: 0

I upgraded my Editor license from 1.9 to 2.0 and I am having an issue with an "object-reference-not-set-to-an-instance-of-an-object " error only when using 2.0.4. When using editor 1.9.6 the code below works adding, editing, and deleting from a SQL Server database. After I upgraded to 2.04, the code allows editing and deleting existing but does not allow adding new records. The error is coming from the Controller at the "return Json(response);" line.

Thank you in advance.

Randy

My controller example code:

using CapEx.Models;
using DataTables;
using Microsoft.AspNetCore.Mvc;
using System;

namespace CapEx.Controllers
{

    public class CapExController : Controller
    {
        [Route("api/capex")]
        [HttpGet]
        [HttpPost]
        public ActionResult CapEx()
        {
            var dbType = Environment.GetEnvironmentVariable("DBTYPE");
            var dbConnection = Environment.GetEnvironmentVariable("DBCONNECTION");

            using var db = new Database(dbType, dbConnection);
            var response = new Editor(db, "Project", "Project.Id")
                .Model<CapExProjectModel>()

                  .Field(new Field("Project.Status")
                    .Options(new Options()
                        .Table("Status")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                   .Field(new Field("Project.Region")
                    .Options(new Options()
                        .Table("Region")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                .Field(new Field("Project.Functional_Area")
                    .Options(new Options()
                        .Table("Functional_Area")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                .Field(new Field("Project.Building")
                    .Options(new Options()
                        .Table("Building")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                 .Field(new Field("Project.Budget_Year")
                    .Options(new Options()
                        .Table("Budget_Year")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                  .Field(new Field("Project.Currency")
                    .Options(new Options()
                        .Table("Currency")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )

                    .Field(new Field("Project.Project_Type")
                    .Options(new Options()
                        .Table("Project_Type")
                        .Value("Id")
                        .Label("Name")
                    )
                    .Validator(Validation.DbValues(new ValidationOpts { Empty = false }))
                )
                    .Field(new Field("Project.Description")
                        .Validator(Validation.NotEmpty(new ValidationOpts { Message = "Description Required!" }))
                        .Validator(Validation.MinMaxLen(5, 85, new ValidationOpts { Message = "Description must be between 5 and 85 charactors!" }))
                )
                    .Field(new Field("Project.Budget_Amount")
                        .Validator(Validation.NotEmpty(new ValidationOpts { Message = "Budget Amount Required!" }))
                )
                .LeftJoin("Status", "Status.Id", "=", "Project.Status")
                .LeftJoin("Region", "Region.Id", "=", "Project.Region")
                .LeftJoin("Functional_Area", "Functional_Area.Id", "=", "Project.Functional_Area")
                .LeftJoin("Building", "Building.Id", "=", "Project.Building")
                .LeftJoin("Budget_Year", "Budget_Year.Id", "=", "Project.Budget_Year")
                .LeftJoin("Currency", "Currency.Id", "=", "Project.Currency")
                .LeftJoin("Project_Type", "Project_Type.Id", "=", "Project.Project_Type")
                .TryCatch(false)
                .Process(Request)
                .Data();
            return Json(response);
        }
    }
}

Model Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CapEx.Models
{
    public class CapExProjectModel
    {
        public class Project
        {
            public int Id { get; set; }
            public int CapExId { get; set; }
            public string CapExNumber { get; set; }
            public string Description { get; set; }
            public int Status { get; set; }
            public int Region { get; set; }
            public int Functional_Area { get; set; }
            public int Building { get; set; }
            public int Budget_Year { get; set; }
            public string Project_Leader { get; set; }
            public string MAR_Number { get; set; }
            public string WBS_Number { get; set; }
            public decimal Budget_Amount { get; set; }
            public int Currency { get; set; }
            public int Project_Type { get; set; }

        }
        public class Status
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Region
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Functional_Area
        {
           public int Id { get; set; }
           public string Name { get; set; }
        }
        public class Building
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Budget_Year
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    public class Currency
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
        public class Project_Type
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin

    Thanks for your question. Which version of 1.9.x was it you upgraded from? Also, are you using .NET Core or .NET Framework?

    Could you add .Debug(true) just before your .Process(...) line and see if that helps the server return valid JSON?

    Thanks,
    Allan

  • rlb5128rlb5128 Posts: 9Questions: 2Answers: 0

    Thank you for the quick turn around. I upgraded from .NET Core using Editor 1.9.6 and upgraded to 2.04. I added the .Debug(true) and I and still getting the error.

    I did a little investigation and found that if I upgrade from 1.9.6 to 2.0.0 or 2.0.1 I can still add projects to the SQL database. When I tried upgrading from 2.0.1 to 2.0.2, I received the same error.

    My JavaScript is below. Maybe you can see something that is incorrect for version 2.0.2 and above.

          $(document).ready(function () {
                editor = new $.fn.dataTable.Editor({
                    ajax: "/api/capex",
                    table: "#example",
    
                    fields: [
    
                        {
                            label: "CapEx Number:",
                            name: "Project.CapExNumber",
                            type: "readonly",
    
    
                        }, {
                            label: "Description:",
                            name: "Project.Description",
    
    
                        },
                        {
                            label: "Project Type:",
                            name: "Project.Project_Type",
                            type: "select"
                        },
                        {
                            label: "Status:",
                            name: "Project.Status",
                            type: "select"
                        }, {
                            label: "Region",
                            name: "Project.Region",
                            type: "select"
                        },
                        {
                            label: "Functional Area:",
                            name: "Project.Functional_Area",
                            type: "select"
                        },
                        {
                            label: "Building:",
                            name: "Project.Building",
                            type: "select"
                        },
                        {
                            label: "Budget Year:",
                            name: "Project.Budget_Year",
                            type: "select"
                        },
                        {
                            label: "Project Leader:",
                            name: "Project.Project_Leader"
                        },
                        {
                            label: "MAR Number:",
                            name: "Project.MAR_Number"
                        },
                        {
                            label: "WBS Number:",
                            name: "Project.WBS_Number"
                        },
                        {
                            label: "Budget:",
                            name: "Project.Budget_Amount"
                        },
                        {
                            label: "Currency:",
                            name: "Project.Currency",
                            type: "select"
                        },
                    ]
    
                });
    
                $('#example').DataTable({
                    dom: "Bfrtip",
                    ajax: {
                        url: "/api/capex",
                        type: 'POST'
                    },
                   // "scrollX": true,
                   // "responsive": true,
                  //  "deferRender": true,
                   // "autoWidth": false,
                  //  "stateSave": true,
                    columns: [
                        { data: "Project.Id" },
                        //{ data: "Project.CapExId", "width": "2%" },
                       { data: "Project.CapExNumber" },
                        {
                            data: "Project.Description",
                            "width": "16em",
                            "render": function (data) {
                                if (data) {
                                    return data.truncate(37);
                                }
                                return;
                            }
                        },
                        { data: "Project_Type.Name" },
                        { data: "Status.Name" },
                        { data: "Region.Name" },
                        { data: "Functional_Area.Name" },
                        { data: "Building.Name" },
                        { data: "Budget_Year.Name" },
                        { data: "Project.Project_Leader" },
                        { data: "Project.MAR_Number" },
                        { data: "Project.WBS_Number" },
                        { data: "Project.Budget_Amount", render: $.fn.dataTable.render.number(',', '.', 0, '$') },
                        //{ data: "Currency.Name"}
                    ],
                    searchPanes: {
                        cascadePanes: true
                    },
                    columnDefs: [{
                        searchPanes: {
                            show: true,
                        },
                        targets: [3, 4, 5, 6, 7, 8],
                    }],
                    select: true,
                    //serverSide: true,
                    buttons: [
                        { extend: "create", editor: editor },
                        { extend: "edit", editor: editor },
                        { extend: "remove", editor: editor }
                    ]
                });
            });
    
  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin

    Many thanks for your investigation into this!

    The only significant change for v2.0.2 in the .NET dll was this one, but I'm not sure why it would be giving you an error and not us.

    Does the error message include a backtrace at all?

    Allan

  • rlb5128rlb5128 Posts: 9Questions: 2Answers: 0

    Hopefully the StackTrace can help you. Thank you for your help.

    System.NullReferenceException
      HResult=0x80004003
      Message=Object reference not set to an instance of an object.
      Source=DataTables-Editor-Server
      StackTrace:
       at DataTables.Editor.PkeyToValue(Dictionary`2 row, Boolean flat)
       at DataTables.Editor._PkeySubmitMerge(String pkeyVal, Dictionary`2 row)
       at DataTables.Editor._Insert(Dictionary`2 values)
       at DataTables.Editor._Process(DtRequest data)
       at DataTables.Editor.Process(DtRequest data)
       at DataTables.Editor.Process(IEnumerable`1 data, String culture)
       at DataTables.Editor.Process(HttpRequest request, String culture)
       at CapEx.Controllers.CapExController.CapEx() in D:\repos\CapExDemo\CapEx\Controllers\CapExController.cs:line 22
    
  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    Answer ✓

    I might have it, could you add:

    .Field(new Field("Project.Id").Set(false)
    

    to your C# Editor field list please? By default fields used from the model are read / write, but this one you don't want to write (no value is submitted from the client-side for it), setting it not writeable will allow the database to compute the value for it (assuming it is a serial).

    Allan

  • rlb5128rlb5128 Posts: 9Questions: 2Answers: 0
    edited June 2021

    Thank you Allen! I added the line to my controller and works perfect.

    Would the fix be the same case for any read-only field or just a column that has Identity specified? An example would be a column in SQL Server generated by a trigger event (i.e. Date_Created)?

  • rlb5128rlb5128 Posts: 9Questions: 2Answers: 0

    I think I answered my own question by running a few simple tests. The only field that is throwing the error is the Id field that is read-only on the server-side. My fields that have triggers are not throwing any errors as they are still writable.

    Allen, thanks again for the help!

  • allanallan Posts: 61,439Questions: 1Answers: 10,053 Site admin
    edited June 2021

    Glad we got to the bottom of it! Yes, I would say that any field you don't want as writeable should have .Set(false) - even if you don't list it in your client-side code, if anyone happened to guess the name of the field, they could attempt to write to it by messing around with the Ajax edit request.

    Allan

  • appliedvisionsappliedvisions Posts: 1Questions: 0Answers: 0
    edited July 2021

    I am having the same issue. I used .Set(false) on my fields and I still get the issue.

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    @appliedvisions Can you post your Editor code please so we can take a look,

    Colin

  • tomleongtomleong Posts: 13Questions: 5Answers: 0

    set the id field to false fixed my problem. i spent 4 hours for the bug until i read the post. Thanks allan!!

Sign In or Register to comment.