Web Services - onSuccess & Errors

I'm learning web services at the moment and have a need to get something into place.

I have produced a basic WebService which is taking 10+ strings and inserting into an SQL table.
[WebMethod]
public string InsertOrder

This as far as I can see allows me to do some checks on what is coming in and return an exception on an error.  But I can only output one error with this.

using XML:SOAP what should I be doing to output multiple errors and should I return a string success on completion or a bool.

Do you have some working code I could just drop into my Service1.asmx.cs file.
I'm not really familiar with having separate classes and referencing them etc.
Is there somethin I could just drop into this file that would output the XML:SOAP errors / success?

Many Thanks.
Andrew.
andrewmilnerAsked:
Who is Participating?
 
HarryNSConnect With a Mentor Commented:
Best way of doing it is,
public class Service1 : System.Web.Services.WebService
    {
        public callStatus1 ResponseStatus;
 
 
        [WebMethod]
        [SoapHeader("ResponseStatus", Direction = SoapHeaderDirection.Out)]
        public string HelloWorldEx(string name, int val)
        {
            ResponseStatus = new callStatus1();
 
            // Your web service Code
                // ON SUCCESS
                ResponseStatus.statusText = "Success";
                ResponseStatus.statusCode = 0;
                return "Success";
                // ON FAILURE
                ResponseStatus.statusText = "Unauthorized Access";
                ResponseStatus.statusCode = 1;
                return "Failed";
        }
    }
 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tempuri.org/")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://tempuri.org/",
    IsNullable = false)]
    public partial class callStatus1 : System.Web.Services.Protocols.SoapHeader
    {
        private string statusTextField;
        private int statusCodeField;
        private System.Xml.XmlAttribute[] anyAttrField;
 
 
        public string statusText
        {
            get
            {
                return this.statusTextField;
            }
            set
            {
                this.statusTextField = value;
            }
        }
        public int statusCode
        {
            get
            {
                return this.statusCodeField;
            }
            set
            {
                this.statusCodeField = value;
            }
        }
        [System.Xml.Serialization.XmlAnyAttributeAttribute()]
        public System.Xml.XmlAttribute[] AnyAttr
        {
            get
            {
                return this.anyAttrField;
            }
            set
            {
                this.anyAttrField = value;
            }
        }
    }
 
 
    //YOUR UI Code
    string result = dhs.HelloWorldEx("blah", 123); 
    int statusCode = dhs.callStatus1Value.statusCode; 
    string statusText = dhs.callStatus1Value.statusText; 
    MessageBox.Show(result); 

Open in new window

0
 
apeterConnect With a Mentor Commented:
Call you web service inside a try catch blocks in your client code. So that if there are any errors you can catch then in ur client code "Catch" block. Below i have sample. Hope it helps.

try
{
      <WebserviceProxyClass> instance = new <WebserviceProxyClass()>;
      string outputmsg = instance.InsertOrder();
      //no exception the flow continues else it goes to catch block
}
Catch(WebException wex)
{
      // you can catch like Timeout, Connection errors
}
Catch(Exception ex)
{
        //Take appropriate action for other than webexception errors.
}
0
 
andrewmilnerAuthor Commented:
I have implemented everything here bar the Your UI Code at the bottom which I couldnt really understand what was for.  whats the dhs referencing.

Also this doesnt appear to be doing anything more than what I was getting with.

catch(exception e)
return(exception.Message)

As it is still only returning one error.
What I'd like to do is to go through my entire strings of input and build the response errors as items within the SOAP response, and then simply return at the end of my web method all of the errors and only return success if error count = 0.

Is that what this code is meant to be doing?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
apeterCommented:
Instead of passing a string, your normal webserice can return a custom class which has all details of the transaction.

The same is done and it is returned in the soap header by andrew. Extend his class "callStatus1" which has more properties to give the more than one result of ur transaction. Like a property which is a array of  statusText and one for Codes

dhs is nothing but in the client, is the name of web service proxy instance name.
0
 
apeterCommented:
Sorry the soad header sample referred by Harry and not by Andrew.
0
 
andrewmilnerAuthor Commented:
Could you help me get started in Extending callStatus1 with the array with some example code and example of sending an error from a catch :-)

Many thanks.
0
 
HarryNSCommented:
1) dhs is just the instance of the webservice.
2) Just you can change this part of your CallStatus1 Class,
private List<string> statusTextField = new List<string>();
        public List<string> statusText
        {
            get
            {
                return this.statusTextField;
            }
            set
            {
                this.statusTextField = value;
            }
        }
3) Whenever you encounter a catch block or wanted to add an error message just add it like,
[WebMethod]
        [SoapHeader("ResponseStatus", Direction = SoapHeaderDirection.Out)]
        public string HelloWorldEx(string name, int val)
        {
            ResponseStatus = new callStatus1();
try
{
//Your Code
// ResponseStatus.statusText.Add("Connection to DB successful"); //Some message you can add in.

}
catch
{
ResponseStatus.statusText.Add("Failed");
ResponseStatus.statusText.Add("Error #1");
ResponseStatus.statusText.Add("Error #2");
}
}
0
 
andrewmilnerAuthor Commented:
Must i only use one try catch block to make this work? i had a toy around yesterday trying it to output multiple lines in the XML:SOAP output but it still just kept outputting one line in the XML.
0
 
andrewmilnerAuthor Commented:
Many thanks for all your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.