Click here to Skip to main content
11,701,855 members (62,600 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi everyone,

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

This is my method in class
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

<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 15-Jan-13 0:33am
v2
Comments
Member 9581488 at 15-Jan-13 16:00pm
   
what is the error?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 314
1 Sergey Alexandrovich Kryukov 310
2 Maciej Los 280
3 Mika Wendelius 119
4 So_Keeee 100
0 OriginalGriff 8,324
1 Sergey Alexandrovich Kryukov 7,422
2 CPallini 4,912
3 Maciej Los 4,196
4 Mika Wendelius 3,556


Advertise | Privacy | Mobile
Web01 | 2.8.150819.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100