Hi,
I have developed a web service in c# which takes a parameter as integer type, but when i call it without passing any value it gives an error which is natural because it cannot convert null into int32 type.
Is there any way i can call this webservice an return a personal error message to the client if he calls the service without passing any value as argument.
I tried to change the int type to int? in the function definition but still i am having the problem , is there any other way?
Hope to get a reply soon.
Thanks,
Jain
Loading
Kirtan PatelPosted Dec 24, 2009, 7:21 AM
you can take Object Array instead of int[] as Parameters Where you can pass any-datatype of variable as below one
public int sqr(params object[] theObjects)
{
}
now you can use method to pass anytpe of variables ;)
sqr("hello",1,2,3,"anything");
as Object is Convertible to any datatype :)
you can convert Object to Int32 By
Convert.ToInt32(objectvar);
for converting object to String
objvar.ToString();
Manish JainPosted Dec 24, 2009, 7:24 AM
I thought of something similar, changing int to string in my function definition and checking if its null i return error message, if it is not null i change it to integer n use it.
Thanks,
Jain
Manish JainPosted Dec 24, 2009, 7:09 AM
yeah i agree its a good way to escape overloading but it is not going to work in my case.
My origional method is
public DataSet getdataset(string userName, string passWord, int Id)
{
}
If i call getdataset(username,password) it returns an exception
cannot convert string to int32 type....
What i want is something like this inside my webmethod
if (id is not passed)
{
DataSet dsErr = new DataSet();
DataTable errTable = dsErr.Tables.Add("Error");
errTable.Columns.Add("ID", typeof(int));
errTable.Columns.Add("Error", typeof(string));
errTable.Rows.Add(1, "my personalised error message");
errTable.AcceptChanges();
return dsErr;
}
thanks a lot
Manish
Kirtan PatelPosted Dec 24, 2009, 7:01 AM
I have came with new Optional Solution if you dont want to create too much methods .
here check the below example i have made for you that method can take as much parameters as you want to pass :)
first check if user dont pass the parameter then lenght of array will be 0 so return the error :)
in this way single method can handle many number of parameters :) from 0 to N... :)
public int sqr(params int[] theObjects)
{
// Check if User passed value or not if length is 0
//then user have not passed the value
// Show the error if length of objects is 0
if (theObjects.Length != 0)
{
int total = 0;
foreach (int i in theObjects)
{
total += i;
}
return total;
}
else
{
return 0;
}
}
Manish JainPosted Dec 24, 2009, 6:43 AM
Thanks for the suggestion, but is there any other way then overloading? Because if i have like 3 or 4 arguments for my method then i have to overload methods for all the combinations :(
Thanks,
Jain
Kirtan PatelPosted Dec 24, 2009, 6:15 AM
Here i m Giving your Problem's Solutions :)
if my answer helps you then please check "do you like this answer" check box :)
you can Define Two Methods With SameName one With parameter and One Without parameter ( Method Overloading )
but in WebService there is Some Modification you have to do to Achieve Method Overloading
I highligheted what you have to change .to achive method overloading in webservice.
so in First method Without Parameter Just return error Messsage to User and In Second method Perform the operation you want to do :)
thats it :)