Solved

'length' is null or not an object

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
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…
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…

911 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