{"id":118,"date":"2021-03-13T00:04:34","date_gmt":"2021-03-13T00:04:34","guid":{"rendered":"https:\/\/wshop.fi\/eng\/?p=118"},"modified":"2021-03-13T00:04:35","modified_gmt":"2021-03-13T00:04:35","slug":"create-and-consume-web-services-in-asp-net","status":"publish","type":"post","link":"https:\/\/wshop.fi\/eng\/create-and-consume-web-services-in-asp-net\/","title":{"rendered":"Create and Consume Web Services In Asp.Net"},"content":{"rendered":"\n<p>In this Article we will learn how to create and consume Web Services In Asp.Net and C#.<\/p>\n\n\n\n<p>A Web Service is platform independent software component, based on Simple Object Access Protocol (SOAP) .<\/p>\n\n\n\n<p>Web Service is used to Exchange of data on the same of disparate system irrespective of architecture.<\/p>\n\n\n\n<p>Web Services Communicate using Standard Web Protocol and data formats like <em>HTTP<\/em>, <em>XML<\/em>, <em>SOAP<\/em><\/p>\n\n\n\n<p>Now See how to create and Consume Web Services In Asp.Net.<\/p>\n\n\n\n<p><strong>Step 1- Create a New Website in Visual Studio.<br>Step 2- Right click on Project, Add New Item -> Select Web Service.<\/strong><\/p>\n\n\n\n<p>Now Open <code>MyWebService.asmx.cs<\/code> file and write the following code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Web.Services;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Data.SqlClient;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">using System.Data;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace WebServicesDemo<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">{<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; [WebService(Namespace = \"http:\/\/tempuri.org\/\")]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; [System.ComponentModel.ToolboxItem(false)]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; public class MyWebService : System.Web.Services.WebService<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp; &nbsp;&nbsp;{<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [WebMethod]<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DataSet GetAllEmployee()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string connectionStr=@\"Data Source=(LocalDB)\\v11.0;AttachDbFilename=C:\\Users\\AMIT\\Documents\\TestDB.mdf;Integrated Security=True;Connect Timeout=30\";<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(connectionStr);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlCommand cmd = new SqlCommand(\"select * from Employee\", con);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SqlDataAdapter da = new SqlDataAdapter(cmd);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataSet ds = new DataSet();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; da.Fill(ds);<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ds;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<p>In above code we are retrieving the Employee List from Employee table.<\/p>\n\n\n\n<p><strong>Step 3- Now Run your application. <\/strong><\/p>\n\n\n\n<p>You will A will be open , copy the url .<\/p>\n\n\n\n<p><strong>Step 4- Add a new <code>WebForm<\/code><\/strong>.<\/p>\n\n\n\n<p><strong>Drag a grid view on It.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"Index.aspx.cs\" Inherits=\"WebServicesDemo.Index\" %&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;!DOCTYPE html&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;head runat=\"server\"&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; &lt;title&gt;&lt;\/title&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;\/head&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;body&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; &lt;form id=\"form1\" runat=\"server\"&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; &lt;div&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;asp:GridView runat=\"server\" ID=\"grdEmployee\"&gt;&lt;\/asp:GridView&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; &lt;\/div&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; &lt;\/form&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;\/body&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;\/html&gt;<\/pre>\n\n\n\n<p><strong>Step 5- Right Click On Project.<\/strong> <\/p>\n\n\n\n<p>Select Add => Service Reference. A popup window will be open .<\/p>\n\n\n\n<p>Paste that copied URL in Address bar, then click on GO.<br>Give any Name to your <code>Service Reference<\/code>.<\/p>\n\n\n\n<p><strong>Step 6- Go to code behind file.<\/strong><br><\/p>\n\n\n\n<p>Create a new Method Bind, which will be use to call the Services Method, and get the result.<\/p>\n\n\n\n<p>Write the following code in code behind file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using System;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">namespace WebServicesDemo<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">{<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; public partial class Index : System.Web.UI.Page<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void Page_Load(object sender, EventArgs e)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!IsPostBack)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bind();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public void Bind()<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MyServiceReference.MyWebServiceSoapClient objService = new MyServiceReference.MyWebServiceSoapClient();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var getEmployee=objService.GetAllEmployee();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grdEmployee.DataSource = getEmployee;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grdEmployee.DataBind();<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;&nbsp;&nbsp; }<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">}<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this Article we will learn how to create and consume Web Services In Asp.Net and C#. A Web Service is platform independent software component, based on Simple Object Access Protocol (SOAP) . Web Service is used to Exchange of data on the same of disparate system irrespective of architecture. Web Services Communicate using Standard [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":119,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,15,19],"tags":[],"class_list":{"0":"post-118","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-coding","8":"category-technologies","9":"category-wordpress"},"_links":{"self":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/comments?post=118"}],"version-history":[{"count":1,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/posts\/118\/revisions\/120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media\/119"}],"wp:attachment":[{"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/media?parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/categories?post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wshop.fi\/eng\/wp-json\/wp\/v2\/tags?post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}