What is Web Service?
Web Service is an application that is designed to interact directly with other applications over the internet. In simple sense, Web Services are means for interacting with objects over the Internet. The Web serivce consumers are able to invoke method calls on remote objects by using SOAP and HTTP over the Web. WebService is language independent and Web Services communicate by using standard web protocols and data formats, such as
• HTTP
• XML
• SOAP
Advantages of Web
Service
Web Service messages are formatted as XML, a standard way for communication between two incompatible system. And this message is sent via HTTP, so that they can reach to any machine on the internet without being blocked by firewall.
Examples for Web
Service
Weather Reporting: You
can use Weather Reporting web service to display weather information in your
personal website.
Stock Quote: You
can display latest update of Share market with Stock Quote on your web site.
News Headline: You
can display latest news update by using News Headline Web Service in your
website.
Creation of web services :
Open visual studio ---> Select File ---> New ---> Web Site ---> select ASP.NET Web Service
Now open your Service.cs file
in web service website to write the code to get the user details from database
Before writing the WebMethod in Service.cs first
add following namespaces
using System.Xml;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
|
After adding namespaces write the
following method GetUserDetails in Service.cs page
[WebMethod]
public XmlElement GetUserDetails(string userName)
{
SqlConnection con
= newSqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd
= new SqlCommand("select * from EXPENSEwhere
UserName like @userName+'%'",
con);
cmd.Parameters.AddWithValue("@userName",
userName);
cmd.ExecuteNonQuery();
SqlDataAdapter da
= new SqlDataAdapter(cmd);
// Create an
instance of DataSet.
DataSet ds
= new DataSet();
da.Fill(ds);
con.Close();
// Return the
DataSet as an XmlElement.
XmlDataDocument xmldata
= new XmlDataDocument(ds);
XmlElement xmlElement
= xmldata.DocumentElement;
return xmlElement;
}
|
Here we need to remember one point that
is adding [WebMethod] before method definition because we need to
access web method pulically otherwise it’s not possible to access method
publically. If you observe above code I converted dataset to XmlElement t
because sometimes we will get error likereturn type dataset invalid type it
must be either an IListSource, IEnumerable, or IDataSourceto avoid this error I converted dataset to
XmlElement.
Here we need to set the database
connection in web.config because
here I am getting database connection from web.config
<connectionStrings>
<add name="dbconnection" connectionString="Data
Source=SHASHANK-PC;Integrated Security=true;Initial Catalog=EXPEN"/>
</connectionStrings>
|
Now run your web service it would be
like this
Our web service is working fine now we need
to know how we can use webservice in our application? Before to know about using web service in
application first Deploy your webservice application in your local system if
you want to know how to deploy application in your local system check this link deploy
application in local system
How to Use Web
service in web application?
By using this webservice we can get the
user details based on username. For that first create one new web application
Open visual studio ---> Select File ---> New ---> Web Site ---> select ASP.NET Web Site
After creation of new website right
click on solution explorer and choose “Add web reference” that would be like
this
Now click on Add Reference button web
service will add successfully. Now open your Default.aspx page and design like
this
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Getting
Data from WebService</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<b>Enter
UserName:</b>
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
<div>
<asp:GridView ID="gvUserDetails" runat="server" EmptyDataText="No Record Found">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
|
Now in code behind add following
namespaces
using System.Data;
using System.Xml;
|
|
After adding namespaces write the
following code in code behind
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindUserDetails("");
}
}
protected void BindUserDetails(string userName)
{
localhost.Service objUserDetails = new localhost.Service();
DataSet dsresult
= new DataSet();
XmlElement exelement
= objUserDetails.GetUserDetails(userName);
if(exelement!=null)
{
XmlNodeReader nodereader
= new XmlNodeReader(exelement);
dsresult.ReadXml(nodereader, XmlReadMode.Auto);
gvUserDetails.DataSource = dsresult;
gvUserDetails.DataBind();
}
else
{
gvUserDetails.DataSource = null;
gvUserDetails.DataBind();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
BindUserDetails(txtUserName.Text);
}
|
3 comments:
THIS IS FOR WEBSERVICES
nice post sir ji.
http://asp-net-by-parijat.blogspot.in/2013/09/web-services-in-aspnet-programming.html
Thanks Parijat sir
Post a Comment