Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to run a webservice from my web page and I'm having a little problem. The webservice is used to populate a second dropdownlist. When an item is selected from the first dropdownlist it calls GetTerritories.
GetTerritories calls the web service TerritoriesService.GetTerritoriesInRegion.

But I’m getting an error specifying "TerritoriesService not defined". It appears that the Javascript function can’t find TerritoriesService. Any ideas as to what’s missing?
I enclosed the web service below.
ASP.NET
<asp:DropDownList ID="lstRegions" Runat="server" Width="210px" 
          DataTextField="label_type" 
          DataValueField="label_type_id" 
          onChange="GetTerritories(this.value);">
        
<asp:DropDownList ID="lstTerritories" Runat="server" Width="275px">
       

 function GetTerritories(label_type_id) {
    
    TerritoriesService.GetTerritoriesInRegion(label_type_id, 
    OnRequestComplete, OnError);
}

C#
//
// this is the web service I'm trying to call
//

using System;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

/// <summary>
/// Summary description for TerritoriesService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class TerritoriesService : System.Web.Services.WebService
{

    [WebMethod()]
    public List<territory> GetTerritoriesInRegion(int label_type_id)
    {
        SqlConnection con = new SqlConnection(
          WebConfigurationManager.ConnectionStrings["Engineering"].ConnectionString);
        SqlCommand cmd = new SqlCommand(
            "SELECT * FROM tblMLPLabelDescription WHERE label_type_id=@label_type_id", con);
        cmd.Parameters.Add(new SqlParameter("@label_type_id", SqlDbType.Int, 4));
        cmd.Parameters["@label_type_id"].Value = label_type_id;

        List<territory> territories = new List<territory>();
        try
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                territories.Add(new Territory(
                  reader["TerritoryID"].ToString(),
                  reader["TerritoryDescription"].ToString()));
            }
            reader.Close();
        }
        catch (SqlException ex)
        {
            // Mask errors.
            throw new ApplicationException("Data error.");
        }
        finally
        {
            con.Close();
        }
        return territories;
    }
}

public class Territory
{
    public string ID;
    public string Description;

    public Territory(string id, string description)
    {
        this.ID = id;
        this.Description = description;
    }

    public Territory() { }
}
Posted
Updated 13-Jul-12 8:38am
v2

See if this[^] article helps.
 
Share this answer
 
put your namespace before TerritoriesService

function InitDropDownList2() {
Example_WebApplication.TerritoriesService.GetTerritoriesInRegion(0, OnRequestComplete, OnError);
}
 
Share this answer
 

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