Wednesday, January 16, 2008

QUESTION: Returning an ArrayList of Custom Objects From a Web Service

I have a current requirement to all a subsidiary to use a current legacy application and interface with my new system to inject data when appropriate. I wanted to create a web service since we are both .Net shops and this would seem to be the easiest method. But I ran into one snafu. I wanted to return to the subsidiary an ArrayList of my Organization objects which contain the orgs id, name etc. so they can use that information in the data insert call.

I have done a lot of looking around but have not found the answer yet to accomplish this. Here is some of the code:

  1. Web service calls an object manager which returns the ArrayList:
    1. ArrayList _array = ObjectManager.Instance.GetAllOrgs();
  2. Object manager calls the object data store returning ArrayList
    1. ObjectStore.Instance.GetAllOrgs();
  3. Object store makes database call, passes reader to method that loops over results building an Org object and adding it to an array.:
    1. public ArrayList GetAllOrgs()
      {
      ArrayList orgs = new ArrayList();

      using (SqlConnection connection = new SqlConnection(_connectionString))
      {
      connection.Open();

      SqlCommand command = new SqlCommand();

      command.Connection = connection;
      command.CommandType = CommandType.StoredProcedure;
      command.CommandText = "usp_GetOrgs";

      SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult);

      orgs = CreateOrgs(reader);

      }

      return orgs;
      }
That is my issue in a nutshell - my web page that consumes the web service does not know how to cope with the ArrayList with Org objects in it.

Any help would be much appreciated!

2 comments:

  1. I had a similar issue calling a web service from an InfoPath form.

    I ended up changing the service to return a simple array of objects instead of an ArrayList.

    This is the post: http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!125.entry


    HTH,

    --Paul Galvin

    ReplyDelete
  2. Why not use Generics to strongly type your collection?

    Like so:
    public List<Organization> GetAllOrgs()
    {
    List<Organization> orgs = new List<Organization>();

    /* Load Collection */

    return orgs;
    }

    ReplyDelete