Thursday, June 19, 2008

Web Services in C#



Web Services in C#



           Web Services are the basic fundamental
building blocks
of invoking features that can be accessed by an application.The
accessibility to provide the features across different platforms
that are developed using different languages, and can be used by any remote application,
over the internet, etc.., makes the web service more useful at times by means of
providing the user only the specific and relevant information, avoiding the accessibility
to resources that have to be securely maintained.





A Simple Web Service Example



           From the Visual Studio 2005 IDE select
the file--> new --> Web Site.  Select an ASP.NET Web Service from the
templates available and name it as "TestingWebServices"







Click Ok.



             Now your service application is
ready to be coded for depending on your requirements. You can see the simple default
webMethod "HelloWorld" to display
the text "Hello World".







        Now we are going to write a simple webmethod to reterive
the Employee details from the "Emp_Details" table and return as "DataSet"
object.






            Before going to run this "web Service" create Database Name : " style="font-size: 10pt; color: #a31515; font-family: Courier New">ForTesting" , Table  Name : "Emp_Details"
for the following details.







       Insert some data like this.







             Now that, your have written your
method, the project needs to be built. To do that right click on your project name
and from the context menu available select the build option which will
complete your service method creation. Now you press "F5" to run the service, It
will open in the IE like this..




               




How to use this web service in Window Appplication 



           This is the application that invokes the Web Service that is defined above.Create a new project, select Visual C#  Windows
Application from the Project Dialog box. Add the "DataGridView"  Control in
the Form.







          Now we add the web Reference in this window application.
Right click the "Reference" Item in the solution Explorer select
the "Add reference".





       Now add the webservice ".asmx" URL then click "GO" button
.







         Click the "Add Reference" Button.
It will add in your projcet like this...







    Now add the following code in the button click event.






      You will get the following Output like this.






No comments: