简体   繁体   中英

WCF Service not loading drop down

I have a wcf service used to populate cascading drop down list in a ASPX page in IIS 7. The drop down list gets thousands of empty option tags but no data. The service does not throw an error. The entire solution runs in IDE on desktop but the service is not working correctly when deployed on IIS. I suspect it is an IIS issue but can not identify it.

<snippet from aspx page>
<asp:DropDownList ID="ddCommercialServicesSiteName" runat="server" Width="150"></asp:DropDownList>
<ajaxToolkit:CascadingDropDown 
    ID="cddCommercialServicesSiteName" TargetControlID="ddCommercialServicesSiteName" 
    PromptText="Select" PromptValue=""   Category="siteID" 
    ServicePath="~/ServiceDropDown.svc" ServiceMethod="GetCommercialSites"
    LoadingText ="Loading..."  runat="server"/>


<!-- ServiceDropDown.svc code  -->
<%@ ServiceHost Language="C#" Debug="true" Service="ReportDashboard.ServiceDropDown" CodeBehind="ServiceDropDown.svc.cs" %>

public List GetCommercialSites(string knownCategoryValues, string contextKey)

{ List sites = new List();

if (contextKey == null)

{

return sites;

} string query = @"select DISTINCT ContactName AS Site , id from sites";

SqlCommand cmd = new SqlCommand(query);

using (SqlConnection con = new SqlConnection(conString))

{
con.Open();

cmd.Connection = con;

cmd.Parameters.Add("@account", SqlDbType.VarChar).Value = contextKey.ToString();

using (SqlDataReader reader = cmd.ExecuteReader())

{

    while (reader.Read())

    {

        sites.Add(new CascadingDropDownNameValue
        {
            name = reader[0].ToString(),
            value = reader[1].ToString(),
        });

    }

    reader.Close();

    con.Close();

}

}

return sites;

}

Try this approach to retrieve values from the Reader.

List<MyModelClass> result = new List<MyModelClass>();



while (reader.Read())

{

            object[] values = new object[3];

            reader.GetValues(values);



            MyModelClass model = new MyModelClass();



            model.ID = values[0].ToString();

            model.ValueProperty = values[1].ToString();

            model.ValueProperty2 = values[2].ToString();



            result.Add(model);

}

In your View/aspx run a jQuery to populate your dropdown,

                            $("#dropdown").change(function () {

                                            dropdown.append($("<option></option>").val("").html("Please wait ..."));



                                            $.ajax({

                                                            url: "/api/CascadingData/GetSomeData/",

                                                            type: "GET",

                                                            dataType: "json",

                                                            data: { Id: Id },

                                                            success: function (d) {

                                                                            dropdown.empty(); // Clear the list, including the please wait option

                                                                            dropdown.append($("<option></option>").val('').html("Select an option..."));

                                                                            $.each(d, function (i, ModelObject) {

                                                                                            dropdown.append($("<option></option>").val(ModelObject.Name).html(ModelObject.Value));

                                                                            });

                                                            },

                                                            error: function () {

                                                                            alert("Error in $('#dropdown').change!");

                                                            }

                                            });

                            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM