Solved

'length' is null or not an object

Posted on 2014-10-27
1
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

724 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