Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

'length' is null or not an object

Posted on 2014-10-27
1
Medium Priority
?
171 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 45

Accepted Solution

by:
Chris Stanyon earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

572 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