Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with webmethod´s int parameters

Status
Not open for further replies.

hacesol

Programmer
Apr 2, 2007
10
ES
Dears:

I´m using a web services with the webmethod below

[WebMethod]
public Client[] GetData(int Val) {

.....

}

On the page for GetData WebMethod, I decide not to type any value for the Val field and when I 'Invoke' the Web Services I receive this: System.ArgumentException: Cannot convert to System.Int32. Parameter name: type ---> System.FormatException: Input string was not in a correct format.....

What´s the best solutions for null values in the int parameters?
 
if the service requires an integer arguement for the webservice, why would you want to call the webservice without the arguement. It seems your webmethod is correct, the problem is how you call the webmethod.

If the integer is required, then provide error checking on the field before calling the webservice.

If the integer should be optional you could try either of the following methods.

1. use nullable types
Code:
[WebMethod]
public Client[] GetData(int? Val)
{
   if (Val.HasValue)
   {
      //GetDataWith Val.Value;
   }
   else
   {
      //GetData without Val
   }
}
when you call the service pass int [tt]MyService.GetData(1);[/tt] or [tt]MyService.GetData(null);[/tt]
2. create 2 signatures
Code:
[WebMethod]
public Client[] GetData(int Val) {...}
[WebMethod]
public Client[] GetData() {...}
this will allow you to call [tt]MyService.GetData(1);[/tt] or [tt]MyService.GetData();[/tt]

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I´m working with Framework 1.1 and with the "int?" statement I´m getting an error.

Is the method 2 the only way?

My webmethod is too long and it has 4 parameters and the first is optional. Do I need to repeat the webmethod for the two situations? :(
 
int? is shorthand for NullableTyp<int> which is .net 2.0 specific. so this won't work.

depending on the valid values of [tt]int Val[/tt], you could pass a value of 0 or -1 to [tt]Val[/tt]. then check this value in the service on whether to use it or not.
Code:
[WebMethod]
public Client[] GetData(int Val, object Val2, object Val3, object Val4)
{
   if (Val > 0)
   {
      //GetDataWith Val, Val2, Val3, Val4;
   }
   else
   {
      //GetData with Val2, Val3, Val4;
   }
}
c# doesn't support optional arguements directly. You need to overload the function with a different set of arguments (optoin 2 above) for "optional" values.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top