Solved

'length' is null or not an object

Posted on 2014-10-27
1
158 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 42

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now