Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method has some invalid arguments?

I am sending data from a windows form to web service in the form of ArrayList. In web service declaration of my method is like:

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

}

and in windows form, on the button click, 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); 
}
  1. Datatype of options is ArrayList and in web service also I am using ArrayList type of variable to hold this value, then why this error occur?

  2. Is it proper way to send parameter to web service or there is an other way for this?

like image 452
Mogli Avatar asked Mar 18 '13 11:03

Mogli


3 Answers

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

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

which is how it's being generated anyway 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 for the initialization of options, if you're looking for something more concise, would be like this:

object[] options = new object[] { "item 1", "item 2", "item 3" };
like image 121
Mike Perrenoud Avatar answered Oct 19 '22 21:10

Mike Perrenoud


I would 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 can't pass complex types like ArrayList, or at least not without some configuration, so just simplify your web service. - Michael

Edit #2

To make your web service use System.Collections.Generic.List

  1. Right click the service in Service References
  2. Configure Service Reference
  3. in the Data Type group
  4. Change the Collection type to System.Collections.Generic.List
like image 33
Orson Avatar answered Oct 19 '22 23:10

Orson


Forget all this changing your code nonsense.

If you right-click the service in the "Service References" folder and select "Configure Service Reference" from the context menu then you can specify which type the client should use for collections.

In your case, just select System.Collections.ArrayList from the "Collection type" dropdown.

However, you could specify System.Collections.Generic.List and have strongly-typed generic lists.

like image 2
Grant Thomas Avatar answered Oct 19 '22 23:10

Grant Thomas