Problem returning a collection from a web service

Sep 18 2007 6:05 AM

hello everyone
i am very new to web services. and i am having a problem returning a collection of custom object from a web method

i have a windows forms application and a web service. the web service contains of two classes . one is 'School' other is a collection of 'School' called 'Schools' which is inherited from CollectionBase. the code for those two classes are as follows

public class School
{
private string _name;
private string _address;
public string SchoolName
{
   get { return _name; }
   s
et { _name = value; }
}
public string Address
{
   
get { return _address; }
   
set { _address = value; }
}
public School()
{
}
}

public class Schools : CollectionBase
{
   public School this[int index]
   {
      
get{return ((School)List[index]);}
      
set{List[index] = value;}
   }

   public int Add(School value)
   {
      
return (List.Add(value));
   }

   public int IndexOf(School value)
   {
      
return (List.IndexOf(value));
   }

   public void Insert(int index, School value)
   {
      List.Insert(index, value);
   }

   public void Remove(School value)
   {
      List.Remove(value);
   }

   public bool Contains(School value)
   {
      
return (List.Contains(value));
   }

   protected override void OnInsert(int index, Object value)
   {
      
// Insert additional code to be run only when inserting values.
   
}

   protected override void OnRemove(int index, Object value)
   {
      /
/ Insert additional code to be run only when removing values.
   
}

   protected override void OnSet(int index, Object oldValue, Object newValue)
   {
      
// Insert additional code to be run only when setting values.
   
}

   protected override void OnValidate(Object value)
   {
      
if (value.GetType() != typeof(School))
         
throw new ArgumentException("value must be of type School.", "value");
}
}

and here is the code written in my webservice

[WebMethod]
public Schools GetSchoolList()
{
   
School school = new School();
   
Schools mySchools = new Schools();
   
for (int i = 0; i < 10; i++)
   {
      school.SchoolName =
"School " + i.ToString();
      school.Address =
"Address " + i.ToString();
      mySchools.Add(school);
   }
   
return mySchools;
}

here is the code in the windows form

private void FrmManageSchools_Load(object sender, EventArgs e)
{
   // web service

   
RegisterSchool rg = new RegisterSchool();
   // this web service has the above web method called GetSchoolList()

   // which returns a Schools Collection. to hold the returned 
   // collection i must have a Schools object like this

   Schools myschools = new Schools();
   myschools = rg.GetSchoolList();

   // but the problem is Schools is not visible, its not available
}

Instead of returning Schools collection if i return School Object it works fine. i have changed my web method and windows forms coding as following

[WebMethod]
public School
GetSchoolList()
{
   
School school = new School();

   school.SchoolName =
"School " ;
   school.Address =
"Address "
 ;
   
return
school;
}

private void FrmManageSchools_Load(object sender, EventArgs e)
{
   // web service

   
RegisterSchool rg = new RegisterSchool();
   // here the school object is available and i can get the data success fully
   School myschool = new School();
   myschool = rg.GetSchoolList();

}

i have tried a lot , but couldnt get to return the Schools collection.
guyz please help me with this.

thank u


Answers (1)