Click here to Skip to main content
16,017,922 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am getting error when loading the dropdown with jquery and json. but am getting an error like 'length' is null or not an object." So please check my below code, and find what is wrong in it. Please help me with my urgent requirement.
C#
<script language="javascript" type="text/javascript">

        debugger;
        $(document).ready(function () {

            $.ajax({

                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "table.aspx/BindDataToDropDown",
                data: "{}",
                dataType: "Json",
                success: function (data) {
                    $.each(data.d, function (value) {
                        $("[id$='ddl_marks']").append($("<option></option>").val(value.CourseLevelid).html(value.CourseLevel));

                        alert("hellow");
                    });
                }
                                error: function ajaxError(response) {
                                    alert(response.status + ' ' + response.statusText);
                                }


            });
        });
    

    </script>


//code
   <pre lang="c#"> [WebMethod]

    public static CourseDetails[] BindDataToDropDown()
    {


        DataTable dt = new System.Data.DataTable();
        List<coursedetails> List = new List<coursedetails>();
        using (SqlConnection con = new SqlConnection( ConfigurationManager.ConnectionStrings["TESTINGUSER"].ToString()))
        {


            using (SqlCommand cmd = new SqlCommand("Select CourseLevelid,CourseLevel from CourseLevels", con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                    CourseDetails course = new CourseDetails();
                    course.CourseLevelID = Convert.ToInt32(dt.Rows[0]["CourseLevelid"]);
                    course.CourseLevel = dt.Rows[0]["CourseLevel"].ToString();
                    List.Add(course);
                }
            }

        }

        return List.ToArray();


    }
    public class CourseDetails
    {
        public int CourseLevelID { get; set; }
        public string CourseLevel { get; set; }
    }
</coursedetails></coursedetails>
Posted
Updated 14-Feb-14 20:08pm
v3
Comments
TryAndSucceed 7-Oct-13 17:08pm    
Why dont you check if you are getting any value back in your jquery? If not, then I think you need to return the List as json value.
Er Daljeet Singh 8-Oct-13 2:49am    
There is problem in you javascript so please modified it.I am also posting soultion.

This is your script,


XML
<script type="text/javascript">



        function abc()
        {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Webmethod.aspx/BindDataToDropDown",
                data: "{}",
                dataType: "Json",
                success: function (data) {


                    $.each(data.d, function (index, value) {
                        var newOption = $('<option>');
                        newOption.attr('value', index).text(value.CourseLevel);
                        $('#DropDownList1').append(newOption);


                    })
                }
            });
        }
    </script>


Here is your HTML code

XML
<form id="form1" runat="server">
   <input type="button" value="Call" id="btn" name="btn" onclick="abc();" />
   <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
   </asp:ScriptManager>
    <asp:DropDownList ID="DropDownList1" runat="server">
    </asp:DropDownList>
    </form>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900