Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.40/5 (3 votes)
See more:
Dear I am trying this. in asp.net webservice. and trying to access at it at client end.


WebService
public class Service1 : System.Web.Services.WebService
    {
        private static string _NPI=string.Empty;
        public static string NPI
        {
            get
            {
                return _NPI;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    _NPI = value;
                }
            }
        }

       
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int a,int b)
        {
            return a + b;
        }

        [WebMethod]
        public int Min(int a, int b)
        {
            return a - b;
        }
    }


Client

ServiceReference1.Service1SoapClient obj = new ServiceReference1.Service1SoapClient();
                int a = obj.Add(5, 8);
                a = obj.Min(a, 10);
            //Methods are accessible but why not i am able to get,set property.
            //Please help?



THANKS
WAITING FOR RESPONSE?
Posted
Comments
Anurag Sinha V 27-Mar-13 1:48am    
Hi,
Functions exposed as [WebMethod] will be available when you add the web service reference..
MuhammadUSman1 27-Mar-13 1:49am    
Yes web service method is accessible but how do i access property.
MuhammadUSman1 27-Mar-13 1:52am    
Anurag Sinha V
Any Idea?
MuhammadUSman1 27-Mar-13 1:48am    
Hello Deverlpoers?
Please Reply...
Anurag Sinha V 27-Mar-13 2:05am    
Hey,

WebMethods expose only methods and not members...
For exposing your members via properties you might wana do something like below:

<pre lang="c#">private string text="Dark Knight";

[WebMethod]
public string SetText(string value)
{
text=value;
return text;
}

[WebMethod]
public string GetText()
{
retrun text;
}</pre>

Hope it helps

-ANurag

1 solution

Hi MuhammadUsman1

since string NPI is static you don need to create object you can access it like this.

ServiceReference1.Service1.NPI="any string";

string var= ServiceReference1.Service1.NPI;
 
Share this answer
 
Comments
MuhammadUSman1 27-Mar-13 5:51am    
Dear i am trying both

public static string NPI { get; set; }
public string sNIP { get; set; }

but both are not accessible in any case.
vinayraghavendra.hs 27-Mar-13 5:58am    
create a object with ServiceReference1.Service1 obj=new ServiceReference1.Service1() and obj.sNPI;.........create object of class ServiceReference1.Service1(important).
vinayraghavendra.hs 27-Mar-13 6:32am    
i know fever about web service according to my research Web services are method-based, so they're not designed to access properties.But there's no reason you couldn't make GetX/SetX methods which are exposed like regular service methods - just make sure you include the [WebMethod] attribute.

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