Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How do I list an array into a DropDownList using jQuery AJAX.
XML
var ddl1SelectedValue = $("#<%= DropDownList1.ClientID %> option:selected").val();
var data2 = { "Value2": ddl1SelectedValue };
var json2 = JSON.stringify(data2);
$.ajax
({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: json2,
    url: "Default.aspx/BindSubfolder2",
    success: function (result2) {
        $.each(result2, function () {
            $("#<%=DropDownList2.ClientID %>").append($("<option></option>").val(this['RemoteURL']).html(this['RemoteNumber']));
        });
    },
    error: function (status, ex) {
        alert("Error Code: Status: " + status + " Ex: " + ex);
    }
});

Error Message:
Error Code: Status: [object Object] Ex: error

C#
[WebMethod()]
public Cascading2[] BindSubfolder2(string Value2)
{
    List<Cascading2> SubFoldetails2 = new List<Cascading2>();
    DataSet ds = new DataSet();
    try
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("Select  [RemoteURL],[RemoteNumber] From [Remote] WHERE RemoteNumber = @RemoteNumber;", conn))
            {
                conn.Open();
                cmd.Parameters.AddWithValue("@RemoteNumber", Value2);
                cmd.ExecuteNonQuery();

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    conn.Close();
                }
            }
        }
        foreach (DataRow DR in ds.Tables[0].Rows)
        {
            Cascading2 cs = new Cascading2();
            cs.RemoteURL = DR["RemoteURL"].ToString();
            cs.RemoteNumber = DR["RemoteNumber"].ToString();
            SubFoldetails2.Add(cs);
        }
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }
    return SubFoldetails2.ToArray();
}

C#
public class Cascading2
{
    public int Remoteid { get; set; }
    public string RemoteNumber { get; set; }
    public string RemoteURL { get; set; }
}
Posted

1 solution

You should get familiar with using the dev tools (f12) or a tool like Fiddler to analyse the traffic between your browser and the server as it often reveals more detailed error, and shows to exactly what data you are sending and receiving.

First off your web method has to be static

public static Cascading2[] BindSubfolder2(string Value2)


Secondly your data is returned as a property called "d".

XML
success: function (result2) {
    $.each(result2.d, function () {
        $("#<%=DropDownList2.ClientID %>").append($("<option></option>").val(this['RemoteURL']).html(this['RemoteNumber']));
    });
},
 
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