A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications. There are three aspects of web service development:
- Creating the web service
- Creating a proxy
- Consuming the web service
Creating the Web Sevice:
A web service is an web application which is basically a class consisting of methods that could be used by other applications. It also follows a code-behind architecture like the ASP.Net web pages, although it does not have an user interface.I am just Creating a Simple WebService.
Take the following steps to create the web service:
Step (1): Select File--> New --> Web Site in Visual Studio, and then select ASP.Net Web Service.
Step (2): A web service file called Service.asmx and its code behind file, Service.cs is created in the App_Code directory of the project.
Step (3): The .asmx file has simply a WebService directive on it:
<%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service"
%>
Step (4): Open the Service.cs file, the code generated in it is the basic Hello World service.
Step (5): Add a New WebMethod in Servie Class named "GetUserDetails". this will fetch all Data from a Table named admission.
WebMethod looks like Below Code Snippet:
[WebMethod]
public DataSet GetUserDetails()
{
SqlConnection
con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
con.Open();
SqlCommand
cmd = new SqlCommand("select * from admission ", con);
cmd.ExecuteNonQuery();
SqlDataAdapter
da = new SqlDataAdapter(cmd);
// Create an
instance of DataSet.
DataSet
ds = new DataSet();
da.Fill(ds);
con.Close();
return
ds;
}
Step (7) : Click on a method name, and check whether it runs properly .Click on GetUserDetails webMethod.
Step (8): Click Invoke Button to Display Output that Service Method returns . like this:
Consuming the Web Service:
For using the web service, create a web site under the same solution. This could be done by right clicking on the Solution name in the Solution Explorer.
Firstly you have to Add service Refrence in your Website. for this Right click on your Website, select Add Service Refrence . in the Add Service Refrence Popup Window Click on Discover Button it will Search all asmx service in your Solution.
now select asmx service that you want to add in your Website and Click OK Button. now Service has been Added in your Website Project. this will look like this :
in Default.aspx add a GridView Control. Code lookes like Below snippet ...
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>
</body>
Now in Default.aspx.cs file Add the Following Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
//this is the proxy
using
ServiceReference1;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
var
proxy = new ServiceReference1.ServiceSoapClient();
grid1.DataSource =
proxy.GetUserDetails();
grid1.DataBind();
}
}
Now Build Your Project and Right click on default.aspx, select View in Browser option. Grid Display all Data From Database Table.
this was simple Web Service Demo.
You Can Download the complete SourceCode from here Download this Project
feel free to Ask any Question regarding this Post.
Thanks and Regards:
Suraj Kumar Maddheshiya
0 comments:
Post a Comment