csharp

How to Create a Simple Webservice in C# and use in ASP.net form Application

In this article, we will learn about how to create simple WebService and How to use it Client application.

Notes–Web service- Web Service is a medium or piece of code to communicate one language in another language or one application to another application

Step 1. Create Web Service
Go to visual studio click on File->Web Site->ASP.Net Empty web site Template
Write the name of Website like (WebServiceDemo)


Step 2. Add a Web Service File(.ASMX)
Click on Project the click on “Add New Item” choose web service.ASMX file
Enter the name of web service.You can see in Screenshot.



Step 3. Put your Code on (.ASMX) Page

[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for MyFirstSevice
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 MyFirstSevice : System.Web.Services.WebService {

public MyFirstSevice () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string UserDetails(string Name, string Address, int Age) {

return "Hello " + Name + " your Address is :" + Address + " and Age is :" + Age;

}

}

[/php]
Screenshot describe after run the create simple web Service

Step 4. Create client application
Go to visual studio click on File->Web Site->ASP.Net Empty web Application Template
Write the name of Web Application like (WebApplicationServiceUse)

Step 5. Add a Web Form(.aspx)
Click on Project the click on “Add New Item” choose web form (.aspx) file
Enter the name of web form
Step 6. Drop three textbox and one button on .aspx page
Follow my Step you can easily do this
[php]
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Address</td>
<td><asp:TextBox ID="txtaddress" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Age</td>
<td><asp:TextBox ID="txtage" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnclick" runat="server" Text="Submit" OnClick="btnclick_Click"></asp:Button></td>
</tr>
<tr>
<td></td>
<td>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
[/php]
Step 47. Finally Add a webrefrence to the Newly created client Application.Follow step through screenshot and click there muouse cursor point
Note->If you have install vs 2015 you will get
“service reference” click on project->Add >Service reference
Then same step follow

Then Click on Advanced Button

After that Click on “Add web Referense” Button

Important Point->After Clicking Add Web reference Button New Window is generated Copy the webservice url and paste
this URL in URL field

Then Click On Go Button

Change The Web Reference Name As you Wish My web Reference Name is MyService after that click on Add reference button

Finally You add a web reference in client Application you can see in screenshot

Final step open .aspx.cs page and use web reference. Copy this code on .aspx.cs page
[php]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnclick_Click(object sender, EventArgs e)
{ //use here web reference
Myservice.MyFirstSevice user = new Myservice.MyFirstSevice();
lblmsg.Text= user.UserDetails(txtname.Text, txtaddress.Text, int.Parse(txtage.Text));
}
}
[/php]
Then run Your project
Note–> First run your web service after that run your new created client project.
Click For Demo

Please suggest me related to this articl.Thank You

Leave a Reply

Your email address will not be published.