Can't SUM datatable column from linked table (MVC)

Can't SUM datatable column from linked table (MVC)

OkellyDokellyOkellyDokelly Posts: 1Questions: 1Answers: 0

Hi there,
I am trying to get the sum of a column in a datatable, where the origin of the data is from a different table than the API used by the datatable.(I am attempting this in ASP.NET MVC). Here is my form:

The datatable is using data from a 'Purchases' table, that is related to the 'Handsets' table, in which the 'SalesPoints' data exists, as you can see from the snippet below. I want to get the sum of the SalesPoints column, however I am experiencing great difficulty. Below is my table code in the Sales View.

$(document).ready(function () {
    var table = $("#purchases"). DataTable({

        rowReorder: {
            selector: 'td:nth-child(2)'
        },


        ajax: {
            url: "/api/purchases",
            dataSrc: ""
        },

        columns: [
            {
                data: "customer.name"
            },
            {
                data: "handset.name"
            },
            {
                data: "handset.salesPoints"
            },
            {
                data: "datePurchased"
            }
        ],
        responsive: true
    });

});

To get this sum, I have included the following code in my Controller:

   private void ComputeBySalesSalesID(DataSet dataSet)
     {
      // Presumes a DataTable named "purchases" that has a column named "handset.salesPoints."
      DataTable table;
      table = dataSet.Tables["purchases"];

    // Declare an object variable.
    object sumObject;
     sumObject = table.Compute("SUM([handset.salesPoints])", "");

      ViewBag.TotalValue = sumObject.ToString();
}

To display this sum, I have included '@Html.Label((String)ViewBag.TotalValue)' in my view; when I run the program, this label throws me an exception, saying it cannot be null.

I would seriously appreciate any insight on this as it is preventing me from progressing with a project that is due quite soon. Thanks!

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    I fear you'll need to ask this on StackOverflow or similar as it appears to be more of a C# issue that DataTables itself (I'm assuming that is C#?).

    It sounds like you need to loop over the data that you want to display and then add information to it - but exactly how you do that will depend upon the language and libraries being used.

    Allan

This discussion has been closed.