Hi with this code I get: cannot implicity convert type string to int.
CalculateSumOfList.ServiceReference1.Service1SoapClient client = new CalculateSumOfList.ServiceReference1.Service1SoapClient();
CalculateSumOfList.ServiceReference1.ArrayOfInt arrayOfInt = new CalculateSumOfList.ServiceReference1.ArrayOfInt();
arrayOfInt.AddRange(listInt);
int result = client.CalculateSum(arrayOfInt); //error here!
label1.Text = Convert.ToString(result);
The webmethod I am calling is here:
namespace CalculateWebServiceSum
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string CalculateSum(List<int> listInt)
{
int[] sum = listInt.ToArray();
return sum.ToString();
}
}
}
The error message is pretty clear. Your method,
CalculateSum()returns a string. On the line that the error occurs, you’re attempting to use the string result to set the value of an int:Option 1
I suggest changing your
CalculateSum()to this:Note: I’m not sure why you’re passing a
List<int>to a web service to be summed. If you haveList<int>, simply callList<int>.Sum(), no WS needed.Option 2
Since you’re calling
.ToString()on the int after retrieving it, why not just retrieve it as a string?