Solved

'length' is null or not an object

Posted on 2014-10-27
1
161 Views
Last Modified: 2014-10-27
Hi I am trying to learn the technique of using a datatable built in a c# webmethod and the displaying the data in an html table by using an AJAX callback in a javascript function.  I can see the data in the alert(result.d); line; however, when I try to pass the result.d into an array and test using:
alert("Length: " + MyArray.length);
I receive an error saying:  Error: 'length' is null or not an object, I need help understanding why?  Thank you in advance ...

//[object Object]

result of alert(result.d);

[
    {
        "WEEK_ENDING": "2014-07-12T00:00:00",
        "MTH": "2014-07",
        "PRODUCT_SUITE": "LD",
        "SEGMENT": "L1",
        "SITE": "USA",
        "SalesAction": "REMOVE",
        "saOrder": 2,
        "ORDERS": 4,
        "TotalCalls": 167,
        "SalesPerCall": 0.023952
    },
    {
        "WEEK_ENDING": "2014-07-12T00:00:00",
        "MTH": "2014-07",
        "PRODUCT_SUITE": "LD",
        "SEGMENT": "L1",
        "SITE": "Canada",
        "SalesAction": "ADD",
        "saOrder": 1,
        "ORDERS": 11,
        "TotalCalls": 187,
        "SalesPerCall": 0.058823
    },
    {
        "WEEK_ENDING": "2014-07-12T00:00:00",
        "MTH": "2014-07",
        "PRODUCT_SUITE": "LD",
        "SEGMENT": "L2",
        "SITE": "Mexico",
        "SalesAction": "ADD",
        "saOrder": 1,
        "ORDERS": 2,
        "TotalCalls": 328,
        "SalesPerCall": 0.006097
    },
    {
        "WEEK_ENDING": "2014-07-12T00:00:00",
        "MTH": "2014-07",
        "PRODUCT_SUITE": "LD",
        "SEGMENT": "L2",
        "SITE": "USA",
        "SalesAction": "REMOVE",
        "saOrder": 2,
        "ORDERS": 19,
        "TotalCalls": 952,
        "SalesPerCall": 0.019957
    }
]

Open in new window

[WebMethod]
    public static string GetAllRecords()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        SqlCommand cmd = new SqlCommand("USP_TOTAL", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        string data = JsonConvert.SerializeObject(dt, Formatting.Indented);
        return data;
    }

}

Open in new window

<head><title>
</title>

    <script src="Scripts/jquery-1.11.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="json2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnLoadData").click(function () {

                $.ajax({
                    url: "GQGrid.aspx/GetAllRecords",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    datatype: JSON,
                    success: function (result) {
                        alert(result.d);
                        var Data = JSON.parse(result.d);
                        var MyArray = Data.Table;
                        alert("Length: " + MyArray.length);
                        var temp = "<table><tr><th>SITE</th>";

                        for (var i = 0; i < MyArray.length; i++)
                        {                           
                            temp += "<tr><td>" + MyArray[i].SITE + "</td></tr>";
                        }                      
                    },
                    error: function (error) {
                        alert(result.responseText);
                    }
                })
            })
        });
      
    </script>
</head>

Open in new window

0
Comment
Question by:Scarlett72
1 Comment
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 40407120
Your result.d is already coming back as JSON so there's no need to parse it - simply check the length and go from there. You also seem to be looking for a property called Table, but I don't see that in your data.

alert("Length: " + result.d.length);
for (var i = 0; i < result.d.length; i++)
{                           
    temp += "<tr><td>" + result.d[i].SITE + "</td></tr>";
}    

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question