Introduction:
Here I will explain what webservice is, uses of webservice and how to create webservice and how to consume webservice in asp.net.
Description:
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
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.
Here I will explain what webservice is, uses of webservice and how to create webservice and how to consume webservice in asp.net.
Description:
Today I am writing article to explain about webservices. First we will see what is webservice is and uses of webservice and then we will see how to use webservice in our applications.
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.
In summary you can any use any web service which is available to use. You can make your own web service and let others use it. Example you can make Free SMS Sending Service with footer with your advertisement, so whosoever use this service indirectly advertise your company... You can apply your ideas in N no. of ways to take advantage of it.
Frequently used word with web services
What is SOAP?
SOAP (simple object access protocol) is a remote function calls that invokes method and execute them on Remote machine and translate the object communication into XML format. In short, SOAP are way by which method calls are translate into XML format and sent via HTTP.
What is WSDL?
WSDL stands for Web Service Description Language, a standard by which a web service can tell clients what messages it accepts and which results it will return.
WSDL stands for Web Service Description Language, a standard by which a web service can tell clients what messages it accepts and which results it will return.
WSDL contains every detail regarding using web service and Method and Properties provided by web service and URLs from which those methods can be accessed and Data Types used.
What is UDDI?
UDDI allows you to find web services by connecting to a directory.
What is Discovery or .Disco Files?
Discovery files are used to group common services together on a web server. Discovery files .Disco and .VsDisco are XML based files that contains link in the form of URLs to resources that provides discovery information for a web service. Disco File contains URL for the WSDL, URL for the documentation and URL to which SOAP messages should be sent.
Before start creating web service first create one table in your database and give name UserInformation in my code I am using same name and enter some dummy data for our testing purpose
Column Name
|
Data Type
|
Allow Nulls
|
UserId
|
Int(Set Identity=true)
|
No
|
UserName
|
Varchar(50)
|
Yes
|
FirstName
|
Varchar(50)
|
Yes
|
LastName
|
Varchar(50)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
Now we will see how to create new web service application in asp.net
Open visual studio ---> Select File ---> New ---> Web Site ---> select ASP.NET Web Service
Now our new web service ready our webservice website like this
|
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 = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation where 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 like return type dataset invalid type it must be either an IListSource, IEnumerable, or IDataSource to 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=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/>
</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
After select Add Web reference option one window will open like this
|
Now enter your locally deployed web service link and click Go button after that your web service will found and window will looks 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);
}
|
Now run your application and check output
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
210 comments :
«Oldest ‹Older 1 – 200 of 210 Newer› Newest»Thank you very much , Suresh I really understood Web Service now.
Greetings from Egypt.
Thank you
Thank you very much, It is very helpful.
very good article for novice in creating and using WebService.....!
How to use this code in windows form to display in datagridview? Please replay.
Thanks...:)
nice article easy to understand for beginers.......
Thanks...good one
thank you very much suresh .
Sir This Artical Is very important to me and my project big problem to solve this artical thanks suresh sir.
rahul shimpi
it is realy useful for me....
Hi good article buddy thank.
Ultimate,sir ji.A lot of thanks for every blog.
U r Superb man
how i use other web services like(cricket score,weather report,new headlines) in my web site
thanx for your post
how to access this webservice via internet
It is very helpful
thank u
I have created the above service & web application now my requirement is to insert user details from Web application to oracle database through web service created above
My Emailid is swapnilnalbalwar05@gmail.com
Good Example, Very Helpful, thanks
how to access this web service via internet
very nice example
Jahabar Sathik Hakeem
Article is very nice.
Nice Article Suresh, Very Clear descriptions..
hi Suresh...
Thanks for such breif explanation.
I like the article and would like to share it with my frnds as well.
Cheers
good one...
Give the example How to use Weather report on my asp.net website
Thank you,
Plz give me complete example on WEBSERVICE Perform All DML operations AND also consume this service in ASP.NET pages perform all operations
help me
hi
getting error on line
localhost.Service objUserDetails = new localhost.Service();
(for localhost)
kindly suggest solution.
thanks sir..i want more example if possible so please share...
hi
sir..
i want to more details about WCF & WEB SERVICE.
hi,
Dear Sir,
Thank in Advanced, i want to more details about WCF if it possible then plz shared...........
braj from Noida.
can you tell me why we use [] in web service .
Please Mail me at shrikantkhode@gmail.com
Such a Good Article
Nice Article.
But i want to call the servicemethod from the aspx page it self by Servicemethod="GetUserdetail"
But the created method for the GetUserdetail is taking one argument.
How can i solve this?
Fine
GAVIDI SRINIVAS 8886164458
FINE
GAVIDI SRINIVAS
9393087430
8886174458
Easy Understandable
So Easy Understandable
hi
can you pls tell me for where this obj created
"localhost". below line:
localhost.Service objUserDetails = new localhost.Service();
I think this is an object!!
Very Nice Article
Good very useful blog
'localhost.Service' does not contain a definition for 'GetUserDetails' ERROR OCCUR using this can you help
Very Nice Article....
Vary Nice Article and easy to understand also well explain...
when i use web service that time Error display like
"Unable to connect to the remote server"
please give me solution
Fabulous work Sir..
Really your articles are so helpful,,amazing ..
Please keep it up Sir ..
Thanks .....
I understood what is websevice. Thank u very much Suresh
its easy to understand
It is very excellent.
Easy to understand for beginners also.
I am really thanks Suresh.
I am so happy, I had a feared of asp.net.Now I am very clear about it. I know within a few months I am also a programmer in one of the biggest concern. Suresh I never forget you till I die!.
Thanks......
thanks .it was of great help.
Thanks a lot..suresh now i clear the concept of web services...and your site is so good and nice artical your website is so so so good ...god bless you.
thanks.its a great example to understand websevices.
i have one error like this
Error is
CS0029: Cannot implicitly convert type 'System.Xml.XmlElement' to 'string'
can any one clear this error
CS0029: Cannot implicitly convert type 'System.Xml.XmlElement' to 'string'
You are rocking
....awsm
i created my webservice by reading this article
tooo good
-Snehal
Really i understood web service from first trial itself.ITS TOO HELPFUL.thanks your nice guidance...
it is very useful for beginners. thanks.
--Srinivas
Thank u...
Very Nice Article to start with WCF
Again i need to say, kya baat kya baat, excellent... :)
Very nice Article ,Thanks...
I am doing a project OpenKm documentation version 6.2.I am having some problem.Can you help me...
Just absolutely brillient!!! You are the man!!!
Cheers,
Shashikant Patil, Pune
This code is very helpful for the beginners
Greate Sir.
Please Add Advance topic MVC,WPF in details
Superb boss, one time recap for web serice
please send me a code for showing Exception message using java script alert message
Nice And Very Good Show code PRoject
thanks agin other web Servies here
Hey Thanks Suresh It's really usefull
Thank you friend i have to implement this web service in wpf using VB.net if you don't mine teach me how to write web service in vb.net using Data set....
Thank you very much for good explaination.
nyc...
Superb Article
it is really interesting.. i have worked out ur sample. thank u so much.... keep doing this kind of artcle.. all the best
thanks.. it seems easy and i did perfectly.. the reson is that u tought us perfectly... thanking you... Shyam Sharma
Great Article for beginner level superb
superb article.....
Thanks a lot....
Very useful.
Thank you sir..
great
Thank you very much for this.
Thanks a lot very simple,understandable.
Indeed It was Good.But Suppose if we use edmx model in place of manual sql use. For that if you can give tutorial it will be great..
Thanks
Thanks very much suresh.
i did not find asp.net web service in vstudio 2010.
Really superb boss...and very easy to understand....great job...
Carry on
very nice article
good job sir. u make it easy
nice example.........
Can you guide me how to code/use web services in vb.net.thanks
Hello Sir ,
Its Very Very Important about for this article i would very thanks to say with pleasure
Getting time out exception when consuming external web service for the second time. I am consuming it in asp.net web application(c#). Any Help...?
It is Really Very helpful to Understand the WEBSERVICES. Its COOL
Really cool article..
Thank you, it help alot
Thank You..Its good article, anyone can understand web service easily..
Great blog...for beginners...thanks guru garu...
good............
Nice one.Its very Useful
Very nice article..
Awesome, Great sample article, post as like this articles, Keep up the good Job.
Thanks
Govindaraj
nice
very good article
great buddy, you explained each and everything in a very simple language and Non Technical person also can understand this...great buddy & Thank You so Much
very nice sir thnaks
very nice
VERY NICE..........
Unable to automatically step into the server.Attaching to the server process failed.Access is denied.
i am getting above error...please help me to solve the problem
I think it is not .Disco file.. It is Disco.exe .. any way good article
Wonderful Suresh ! Every time you are describing in this way that's a newly born child also learn. Keep it Up. Also writes articles on WPF,WCF, MVC etc.
Nice article easy to understand .............
Nice article easy to understand .............
Very good article for the beginners to study about webservice...good work
XML Parsing Error: not well-formed
i getting above mentioned error when i run my default page in inetmgr
Thanks for giving good example
how to generate http error code 100 if nodata
and http error code if data is successfully recieved
and 102 is other errors
...
Pls Respond me
its helpfull budddy........
Thanks for giving good example
its very helpful...thank u
Hi suresh,
I copied my webservice published folder in C:\inetpub\wwwroot folder..But in inetmgr m not getting any default website...wat i can do???
nice
Thank you very much for your all articles.
Informative..
Thank you so much sir. I really thought that web services is a hard topic but your way of explaining web services really helps me a lot.
Thanking you once again.
Rajesh Tiwari(Pune)
thank you so much suresh...
Thank you Suresh its really gd & easy after taking this example.Pls explain about WCF, WPF, Link & MVC.....etc
Thanks. Web service have ultimate performance
10q Very Much Mr.Suresh
Defenitely a standard example. Thanks
Very good tuto!
Very Helpful for Web Service concept.Thnx Suresh.
Plz post simple ASP.Net web login with ms sql data.
From
Soumen R
Nice article , very helpful to me.
Awesome
excellent
Nice Article.....
Hi Sir,
I'm Raghavendra. I've following your blog from many days and it helped me a lot in my daily project routines.
Recently I've got an error in a windows application that I've worked on it earlier.
Coming to the point I've developed a windows application in which client has asked to make it demo version for 30 days and later user should buy it from the site. I've did the same. On installing the exe I've created a registry with my application name along with date of installation. Later every time when clicked on exe it will check with the server data to the installed date and checks whether it has crossed 30days. For getting the date I've used a php service and added it as reference.
This worked very fine earlier. I've added the web reference in my winforms and accessed the method and checked in my application.
But now itsn't running. I've removed service reference and tried to add the same wsdl link again but it says
"The HTML document does not contain Web service discovery information."
Here is the service reference link:
http://projects.santabantathegreat.com/PSMan/license/LicenseService.php?class=licenseService.wsdl
Please help me in this issue ASAP and let me know the source of the problem whether it is the code or the service??
.NET version: VS 2008
Nice article and Thanks
its clear
Its Very Nice Blog.
Thank u So much.......!!!!!!!!1
simple explanation throughout the article.
great:)
sukriya Suresh Saab
bad one
ravi
thanks it's helpful
where we will get username in service.asmx.cs file . can u please tell me ?
Sir its really good article. Very helpfull to me.
Thank you sir, it's very helpfull
How to add web service in android application named. Mobile banking
Excellent artical...
it is very useful for me.. thnq u sir...
getting problem with localhost how to salve that problem
localhost.Service obj = new localhost.Service();
Thanks, i got cleared idea about web services....
Thanks a lot ,,, It helped me a lot Madarchod
Very Useful Example.
Its really useful for me to understand the basic concept of web service. Thank you so much.
thanks sir.....such a great work.
Hi suresh i calling my wevservice through ajax call in javascript in which i have return string in which 3 different condition in javascript like
if(data.d="1")
{}
else if(data.d="2")
{}
else
{}
in which local host it give proper return value but in live site it return always 1 then how can i solved it its emergency pls give me a reply
it was really helpful thanks
Thanx.....
thanks...great explanation...its really helped me lot
Thanks........... It is good for learn
Super Explaination.Thanks.
Very useful information!!!!
It's a really good helpful article.
thanks admin nice shares..
Thanks........... It is good for learn
very informative and good tutorial
Super Explaination.Thanks.
good
Easy example.
Thank you sir..
its nice article for begginers
Demonstrate authentication and authorization as well please!
Thank you ......
really helpful to understand create and consume webservice
Super Article Suresh, Very Clear descriptions.Thanks for updating .
Nice Article Suresh, Very Clear descriptions
Really nice article, easy to understand.. Thanks
Please can you post articles on MVC concept..
Really Thanks..
nice...
for the error: type or namespace name localhost could not be found....
in vs 2012, we didn't get direct option as "add web reference" but we can have "add service reference".
In such cases just click on add service reference.. click on advanced button (under the namespace textbox) then click ok. Next window will come then click add service reference. specify URL address of your service.
thanku
nice article , MVC Sample available?
Hi Suresh
Can u explain what is localhost in the above code.
Plz any one help me. i was stuck their.
Hello sir,
Can you help me, after making web service how can use that service
Superb article ...............
Nice
mmmm
it is very good article provided by you .
Good article .........Able to grab it easily ..Thanks Man
Ur articles are very nice ,easy to understand Thanq
small and sweet understandable format. thanks
Note: Only a member of this blog may post a comment.