Reputation: 2012
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);
}
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?
Is it proper way to send parameter to web service or there is an other way for this?
Upvotes: 6
Views: 2829
Reputation: 15441
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
Upvotes: 4
Reputation: 45083
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.
Upvotes: 2
Reputation: 67898
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" };
Upvotes: 8
Reputation: 22945
I would use a typed list or array in your web method definition. No confusion possible. An arraylist is not strongly typed, which means that the contents cannot be known until run-time.
[WebMethod]
public int SaveSelectedOffers(string[] offers, int selectedRows)
{
}
Upvotes: 0