Does the method have some invalid arguments?

I am sending data from a window form to a web service in the form of an ArrayList . In the web service declaration, my method is similar:

 [WebMethod] public int SaveSelectedOffers(ArrayList offers, int selectedRows) { } 

and in the form of a window, when I click a button, my code is:

 private void offersAvailableSubmit_Click(object sender, EventArgs e) { ArrayList options; options.Add("item 1"); options.Add("item 2"); options.Add("item 2"); //In this line of code it is showing error that Argument 1: cannot convert from 'System.Collections.ArrayList' to 'object[]' int rowsAffected = serviceCaller.SaveSelectedOffers(options, rowCount); } 
  • The data type of the parameters is ArrayList , and in the web service I also use the variable type ArrayList to store this value, why this error occurs?

  • Is it sending the parameter to the web service correctly, or is there another way to do this?

+6
source share
4 answers

Web services cannot pass complex types like ArrayList , or at least not without some configuration, so just simplify your web service. Change it like this:

 public int SaveSelectedOffers(object[] offers, int selectedRows) 

, which is somehow generated one way or another, as you can see , and then call it like this:

 private void offersAvailableSubmit_Click(object sender, EventArgs e) { object[] options = new object[3]; options[0] = "item 1"; options[1] = "item 2"; options[2] = "item 2"; int rowsAffected = serviceCaller.SaveSelectedOffers(options, rowCount); } 

Another option initialization options , if you are looking for something more concise, would be as follows:

 object[] options = new object[] { "item 1", "item 2", "item 3" }; 
+8
source

I suggest you use

 [WebMethod] public int SaveSelectedOffers(IList<string> offers, int selectedRows) { } private void offersAvailableSubmit_Click(object sender, EventArgs e) { IList<string> options = new List<string>(); options.Add("item 1"); options.Add("item 2"); options.Add("item 2"); int rowsAffected = serviceCaller.SaveSelectedOffers(options, rowCount); } 

Edit # 1

Well said by Michael:

Web services cannot pass complex types like ArrayList, or at least not without some configuration, so just simplify your web service. - Michael

Edit # 2

So that your web service uses System.Collections.Generic.List

  • Right-click a service in service links
  • Setting Up Service Help
  • in the Data Type group
  • Change the collection type to System.Collections.Generic.List
+4
source

Forget it by changing your code.

If you right-click a service in the Service Links folder and select Configure Service Link in the context menu, you can specify which type the client should use for collections.

In your case, just select System.Collections.ArrayList from the "Collection Type" drop-down menu.

However, you can specify System.Collections.Generic.List and have strongly typed shared lists.

+2
source

I would use a typed list or array in the definition of your web method. Confusion is possible. The arraialist is not strongly typed, which means that the contents cannot be known until runtime.

 [WebMethod] public int SaveSelectedOffers(string[] offers, int selectedRows) { } 
0
source

All Articles