0

I have a Kendo grid that I largely copied from a co-worker (the difference being that they use different tables as their source) and his works. He used a dynamic source instead of a model.

When I access the page, the grid doesn't appear. The ReadCustomerNotes action has a data table with data, so that's not the problem. I added a try-catch block and am now getting the error "Cannot perform runtime binding on a null reference".

I couldn't find anything online dealing with this error and a Kendo Grid using a dynamic source. How can I find out where the null reference is? Is there something I can put in the catch block to show what is causing the error?

Here's my code:

AdminCustomerNotes.cshtml

@{
  ViewBag.Title = "AdminCustomerNotes";
 }

@using (Html.BeginForm("AdminCustomerNotes", "Admin"))
{
 @(Html.Kendo().Grid<dynamic>()
 .Name("Grid")
 .Columns(columns =>
  {
   foreach (System.Data.DataColumn column in Model.Columns)
   {
    switch (column.ColumnName)
    {
     case "customerNotes":
      columns.Bound(column.ColumnName);
     break;
    }
   }
  })
  .Pageable(x => x.PageSizes(new int[] { 10, 20, 30, 50 }).Refresh(true))
  .Sortable()
  .Filterable()
  .Groupable()
  .DataSource(dataSource => dataSource
  .Ajax()
  .Model(model =>
   {
    var id = Model.PrimaryKey[0].ColumnName;
    model.Id(id);
   })
  .Read(read => read.Action("ReadCustomerNotes", "Admin", new { customerID = int.Parse(ViewBag.compId.ToString()) })))
  .AutoBind(true)
 )
}
catch (Exception ex)
{
 @ex.Message;
}
}

AdminController.cs

    public ActionResult ReadCustomerNotes([DataSourceRequest] DataSourceRequest request, int customerID)
    {
        var customerNotes = CustomerNotes(false, customerID);
        return Json(customerNotes.ToDataSourceResult(request));
    }
    private DataTable CustomerNotes(bool init, int customerID)
    {
        try
        {
            return Rep.GetCustomerNotesByCustomerID(init ? 0 : customerID);
        }
        catch (Exception exception)
        {
            return null;
        }
    }

Repository.cs

public DataTable GetCustomerNotesByCustomerID(int customerID)
{
  try
  {
   var dt = new DataTable();
   dt.Columns.Add("customerNotesID", typeof(int));
   dt.Columns.Add("customerNotesDate", typeof(string));
   dt.Columns.Add("customerNotes", typeof(string));
   dt.PrimaryKey = new[]
   {
    dt.Columns["customerNotesID"]
   };
   var qry =
    from customerNotes in dat.tblCoNoteses
    where (
           customerNotes.CompId == customerID
          )
    select new
    {
        customerNotes.CoNotesId
     ,  customerNotes.CompId
     ,  customerNotes.Note 
     ,  customerNotes.AddBy
     , customerNotes.AddDate
     };
     foreach (var itm in qry)
     {
       var row = dt.NewRow();
       row["customerNotesID"] = itm.CoNotesId;
       row["customerNotesDate"] = string.IsNullOrEmpty(itm.AddDate.ToString()) ? "" : DateTime.Parse(itm.AddDate.ToString()).ToShortDateString();
       row["customerNotes"] = itm.Note;
       dt.Rows.Add(row);
     }
    return dt;
  }
 catch (Exception exception)
 {
   return null;
 }
}
Nicholas
  • 10,490
  • 19
  • 67
  • 85
boilers222
  • 1,728
  • 7
  • 29
  • 55
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jun 18 '15 at 01:28
  • I know what a NullReferenceException is. I was asking how to find out what was causing it in the Kendo grid. Thanks! – boilers222 Jun 18 '15 at 12:43
  • If you read that question, you'll also see "how do I fix it" – John Saunders Jun 18 '15 at 15:12

1 Answers1

0

I think Model is null, in your foreach (System.Data.DataColumn column in Model.Columns) statement. Model references to the @model ... at the top of the view, not the grid's model.

You can do something like this (haven't tested it):

Action

public ActionResult AdminCustomerNotes() {
    var customerNotes = CustomerNotes(false, customerID);
    return View("AdminCustomerNotes", customerNotes);
}

View

@model DataTable

@(Html.Kendo().Grid(Model)
    .Name("Grid")
    .Columns(columns =>
    {
        foreach (System.Data.DataColumn column in Model.Columns)
        {
            switch (column.ColumnName)
            {
                case "customerNotes":
                    columns.Bound(column.ColumnName);
                    break;
            }
        }
    })
    .Pageable(x => x.PageSizes(new int[] { 10, 20, 30, 50 }).Refresh(true))
    .Sortable()
    .Filterable()
    .Groupable()
)
Nicholas
  • 10,490
  • 19
  • 67
  • 85
  • Thanks Nicholas for the reply. There is no model at the top. This is not a strongly typed view. Not how I would have done it, but my co-worker started created the app and now I have to follow his example. I think the dynamic tag and the model defined in the grid is supposed to create a model "on the fly" based on the data from the data source. I just don't know why it's not working. – boilers222 Jun 18 '15 at 11:55