Click here to Skip to main content
14,882,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my web service which i used to retrieve data from database..in this GetCountries,GetCities
bind the data but GetVillages give the below error..
Method Error 500..i tird blow code plzzzz help me

C#
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ServiceCS : System.Web.Services.WebService
{
   [WebMethod]
   public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues)
   {
      string query = "SELECT DISTINCT Country FROM Customers";
      List<cascadingdropdownnamevalue> countries = GetData(query);
      return countries.ToArray();
   }

   [WebMethod]
   public CascadingDropDownNameValue[] GetCities(string knownCategoryValues)
   {
      string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["Country"];
      string query = string.Format("SELECT DISTINCT City FROM Customers WHERE Country = '{0}'", country);
      List<cascadingdropdownnamevalue> cities = GetData(query);
      return cities.ToArray();
   }

   [WebMethod]
   public CascadingDropDownNameValue[] GetVillages(string knownCategoryValues)
   {
      string village = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["village"];
      string query = string.Format("SELECT DISTINCT village FROM Customers WHERE City = '{1}'", village);
      List<cascadingdropdownnamevalue> vilgs = GetData(query);
      return vilgs.ToArray();
   }

   private List<cascadingdropdownnamevalue> GetData(string query)
   {
      string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
      SqlCommand cmd = new SqlCommand(query);
      List<cascadingdropdownnamevalue> values = new List<cascadingdropdownnamevalue>();
      using (SqlConnection con = new SqlConnection(conString))
      {
         con.Open();
         cmd.Connection = con;
         using (SqlDataReader reader = cmd.ExecuteReader())
         {
            while (reader.Read())
            {
               values.Add(new CascadingDropDownNameValue
               {
                  name = reader[0].ToString(),
                  value = reader[0].ToString()
               });
            }
            reader.Close();
            con.Close();
            return values;
         }
      }
   }
}
Posted
Updated 15-Dec-13 2:30am
v3
Comments
   
Can you show the Service Interface code?

1 solution

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ServiceCS : System.Web.Services.WebService
{
[WebMethod]
public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues)
{
string query = "SELECT DISTINCT Country FROM Customers";
List<cascadingdropdownnamevalue> countries = GetData(query);
return countries.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetCities(string knownCategoryValues)
{
string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["Country"];
string query = string.Format("SELECT DISTINCT City FROM Customers WHERE Country = '{0}'", country);
List<cascadingdropdownnamevalue> cities = GetData(query);
return cities.ToArray();
}

[WebMethod]
public CascadingDropDownNameValue[] GetVillages(string knownCategoryValues)
{
string cities= CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["City "];
string query = string.Format("SELECT DISTINCT village FROM Customers WHERE City = '{0}'", cities);
List<cascadingdropdownnamevalue> vilgs = GetData(query);
return vilgs.ToArray();
}

private List<cascadingdropdownnamevalue> GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
List<cascadingdropdownnamevalue> values = new List<cascadingdropdownnamevalue>();
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
cmd.Connection = con;
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
values.Add(new CascadingDropDownNameValue
{
name = reader[0].ToString(),
value = reader[0].ToString()
});
}
reader.Close();
con.Close();
return values;
}
}
}
}
   

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