Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi everyone,

This is my webservice method.
C#
[WebMethod]
    public List<cls_Countries> GetCountries()
    {
        cls_GetLocations locations = new cls_GetLocations();
        return locations.GetCountries();
    }


This is my method in class
C#
public cls_GetLocations()
        {
        }
        public List<cls_Countries> GetCountries()
        {
            List<cls_Countries> list=new List<cls_Countries>();
            try
            {
                DataTable dt= SqlHelper.ExecuteDataset(SqlHelper.ConnString, CommandType.StoredProcedure, "Proc_GetCountries").Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    cls_Countries country = new cls_Countries();
                    country.CountryID = dr["CountryId"].ToString();
                    country.CountryName = dr["Country"].ToString();
                    list.Add(country);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return list;
        }


I want to bind to drop down I tried below code but it wont work so please solve and provide me correct solution

JavaScript
<script type="text/javascript" >

    $(function () {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: 'WebServices/AutoComplete.asmx/GetCountries',
            data: "{}",
            datatype: "json",
            success:
                function (data, event) {
                    $.map(data.d, function (item) {
                        alert(item);
                        //$("#<%= drpCountry.ClientID %>").index = item.countryid;
                        //$("#<%= drpCountry.ClientID %>").val = item.countryname;
                        //alert(item.countryname);
                    });
                }

        });

    });                                                         
</script>
Posted
v2
Comments
Member 9581488 15-Jan-13 16:00pm    
what is the error?

1 solution

XML
List<Customer> _customer = new CustomerBLL().GetAllCustomers();
  if (_customer != null)
    {
    ddlCustomer.DataSource = _customer;
    ddlCustomer.DataValueField = "CustomerId";
    ddlCustomer.DataTextField = "CustomerName";
    ddlCustomer.DataBind();
    ddlCustomer.Items.Insert(0, new ListItem("Select Customer", "0"));
    }


Try to use this code.
Here GetAllCustomers(); is the method which returns List of customer and the method is in CustomerBLL() class. The method returns CustomerId, CustomerName & so on as a List. In the drop-down list i'm binding CustomerId as "DataValueField" & CustomerName as "DataTextField". And the last of all i'm adding "Select Customer" at the top of the drop-down list, thought you may need it so i add it too.

Hope it would help you.

Thanks
 
Share this answer
 
v2

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