Click here to Skip to main content
15,904,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService2
{
    /// <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 int add( int a, int b)
        {
            if (a > b)
            {

            }
            int c;
            c = a + b;
            return c;
        }
    }
}
Posted
Comments
KaushalJB 2-Dec-14 1:22am    
Unclear !!

you can return object, create class like below

C#
public class Results
{
   public string Message {get; set;}
   public int Value {get; set;}
}


then
C#
[WebMethod]
 public Results  add( int a, int b)
 {
     Results res new Results();  
     if (a > b)
     {
         res.Message ="a is grater than b";
     }

     res.Value = a + b;
     return res;
 }

when you call
C#
localhost.Service1 client = new localhost.Service1();
int a = int.Parse(TextBox1.Text);
int b = int.Parse(TextBox2.Text);
var res = client.add(a, b);
LabelValue.Text = "Add Result : " + res.Value;
LabelMessage.Text = "Add Message : " + res.Message;
 
Share this answer
 
v3
Comments
DamithSL 2-Dec-14 1:42am    
Thank you Tadit
Welcome bro. :)
Member 10874581 2-Dec-14 2:49am    
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service1 tt = new localhost.Service1();
int a = int.Parse(TextBox1.Text);
int b = int.Parse(TextBox2.Text);
int yy = tt.add(a, b);
Label3.Text = "add" + yy.ToString();




}
error is coming
DamithSL 2-Dec-14 3:23am    
after you update your web method, right click your web service reference and update it, then it will reflect web service changes to your client application.

check my updated answer, you will get object with two properties ( message and value)
C#
[WebMethod]
 public String add( int a, int b)
 {
   String res="";
     if (a > b)
     {
         res="a is grater than b";
     }
     return res;
 }
 
Share this answer
 
You can throw the exception by using SOAP FAULT message. See article about soap fault
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900