Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Wednesday, 22 March 2017

Calling (Consume) Web Service In Asp.Net Using C#

 Webservice     No comments   


Introduction
In this post we will learn how to consume a Web Service in asp.net using Visual Studio 2015. How to add service reference and and also detail about the basic terms used in Service like end points etc.


Description
 VS 2015, VS 2012, VS 2013 doesn't allows you to add a service reference directly from references . VS 2015 has some new advance features. In previous post we learned how to create a Web Service now we learn how to run this service with our application.

Practice
Just like service creation , service consumption also done in 5 simple steps.

1. Add a New WebSite, give a suitable name to it (in my example application name is ImplementService ) and click OK.

2. Right Click on Website name ,mouse over on Add and Select Add Service reference.          You will find the similar screen like below image


3. Run your service project which you created earlier and copy the service URL and click on   Go. Your Web Method should display in left side panel. If you did not create service yet please read this post first Create Web Service In Asp.Net Using VS 2015 .


4. Click on Advance and then click on Add Web Reference and paste your service URL here       and press enter.  Your service methods displayed in front of you .Change service reference name ,here i used GetAgeService and click on Add reference.



5. Your service reference added successfully now just create a simple aspx WebForm in your application and  write code for getting output from service method call.


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" 
         Inherits="Home" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
        <asp:Button ID="btnGetAge" runat="server" Text="Get Age"
                    OnClick="btnGetAge_Click"/>
        <span style="margin-left:90px;"></span>
         <asp:Label ID="lblAge" runat="server" Font-Bold="true"> </asp:Label>
    </div>
    </form>
</body>
</html>

And Use this code on code behind  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GetAgeService;
 
public partial class Home : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {      
    }
   protected void btnGetAge_Click(object sender, EventArgs e)
   {
       if (string.IsNullOrEmpty(txtDOB.Text))
           lblAge.Text = "Please enter the Dob";
 
       TestService objService = new TestService();
       String Age = objService.GetAge(Convert.ToDateTime(txtDOB.Text));
       lblAge.Text = "Your Age is "+ Age;
   }
 }



In the above code i added GetAgeService on namespace, which is service refrence name given earlier at the time of Add Reference. 

On the button click first we need to create a service object . By adding namespace   GetAgeService , TestService is accessible.in project. Create the object of service and access the web method . 
Run the Project and Enter DOB in DD/MM/YYYY format in TextBox and click on GetAge button. And you get the Age in output based upon entered DOB.










Hope this post will be very helpful for you. If you have any query related web services please let comment down below.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write