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
- HTTP
- XML
- SOAP
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.
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.
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 |
using System.Xml; using System.Configuration; using System.Data; using System.Data.SqlClient; |
[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; } |
<connectionStrings> <add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/> </connectionStrings> |
![]() |
![]() |
<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> |
using System.Data; using System.Xml; | |
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); } |
![]() |
|
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. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|








Subscribe by RSS
Subscribe by Email
103 comments :
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 .....
good
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