C# 2005 Web Service question

Hello everyone,

I'm fairly new to C# and I have a question about return types. Specifically, I have a web method returning a dataset, but I need to return an error message if there is one. How is this done in C# Thanks!

Robert Lewis



Answer this question

C# 2005 Web Service question

  • Raptorix

    you have a couple of options.

    Give the method an "out" parameter, being a string to indicate an error message and return null if there is an error and set the "out" parameter to the error message you want.

    Example:

    [WebMethod]

    public DataSet MyMethod(out string theErrorMsg, type param, type param2.........)

    {

    //psuedo:

    if (error)

    {

    theErrorMessage = "There was an error";

    return null;

    }

    else

    {

    theErrorMessage = String.Empty; //no error

    }

    }

    so when you are calling from your client:

    string errorMsg = String.Empty;

    DataSet theDataSet = MyWebService.MyMethod(out errorMsg, param, param......);

    if (theDataSet == null)

    {

    MessageBox.Show("Error: " + errorMsg);

    }

    does this make sense

    Alternatively you could make a global variable in the webservice which you can access directly to check for any errors, and set errors in the webservice itself but that wouldnt be as great (could forget to reset the variable to empty or whatever and would get yourself/user confused with the error messages)



  • Soren D.

    :-) Good points. I'm not an expert on webservices so I apologise.

    try the whole "out" approach and see how that goes.

    you could create a class object which has:

  • DataSet

  • errorMsg

     

    so it returns this "info" class containing the dataset and an errorMsg. If DataSet returned is null then set the errorMsg to the error message and show that to your client. This is one other way I can think of at this moment.

    You could set an error message in the datatable in the dataset but that would just be overkill and not good to do



  • stefan_sp

    >> Give the method an "out" parameter, being a string to indicate an error message and return null if there is an error and set the "out" parameter to the error message you want. <<

    Does an out parameter work on a web service Since the parameters & response are being marshalled through HTTP, I'd figure each would only go one way.

    >> Alternatively you could make a global variable in the webservice which you can access directly to check for any errors, and set errors in the webservice itself but that wouldnt be as great (could forget to reset the variable to empty or whatever and would get yourself/user confused with the error messages) <<

    Well, that's defnitely not going to work, as a webservice is intended to be accessed across domains (ie, my application access the webservice on your website.) To access a global varaible, the reader and the varaible would have to be in the same process. For a webservice, we have separate processes on separate machine on potenially opposite ends of the Internet.



  • jeffmorris

    It would have to be in the dataset. Add a second DataTable to the DataSet, called, say "Messages". Messages would have one DataColumn, "Message". Put the success or failure message there.



  • C# 2005 Web Service question