Click here to Skip to main content
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
Edited 15-Jan-13 0:36am
Tadit Dash84.2K
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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 1,518
1 Manas Bhardwaj 319
2 Tadit Dash 285
3 OriginalGriff 273
4 Peter Leow 205
0 Sergey Alexandrovich Kryukov 9,530
1 OriginalGriff 5,716
2 Peter Leow 4,345
3 Maciej Los 3,540
4 Abhinav S 3,373


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid