Consuming a Web Service from a Win Form Application
Consuming a Web Service from a Win Form Application
Saurabh Nandu
April 12 2001
Level: Beginner/Intermediate
Download Source Code
Introduction
In the previous .NET101 article Building a Stock-Quotes Web
Service, we saw how to build a Stock-Quotes Web Service and consume it from a
ASP.NET web page. In this article we will concentrate on consuming the
Stock-Quotes Web Service from a Win Form Application.
This article
assumes that you have already created a Web Service and hosted it in a
virtual directory called stockquote. i.e. the full path to the Web Service is
http://dotnet101.com/stockquote/stockquote.asmx.
Also we shall be using VS.NET to build our Win Form.
Building a Win Form Application
Create a new Project
Start-up VS.NET and Start a new Project from File ->New ->Project ->Visual C# Projects->Windows Application. In the Name field for the Application enter StockQuoteConsume. The Design View of VS.NET changes as shown below.
Figure 1
Adding Components
Now drop a TextBox, a Button and a ListBox on the Win Form in Design view from the Toolbox as shown below.
Figure 2
Adding a Web Reference
To consume a Web Service from VS.NET you have to add a Web Reference to the Web Service. To add a Web Reference to our Stock-Quotes Web Service, go to the Project menu -> Add Web Reference. A dialog box which shows up is shown below. Enter the Location (URI) path to the Stock-Quotes Web Service in the Address textbox and click Enter. Once VS.NET discovers the Web Service click Add Reference to add a Web Reference to the Stock-Quotes to our Project. While adding Web Reference, VS.NET automatically generates all the necessary plumbing code like Proxy class generation etc required to consume the Web Service
Figure 3
The Solution Window gets updated like below to reflect the
Web Service reference added.
Figure 4
Wire-Up the Button Event-Handler
Double-Click on the button on the Win Form in design window to write the event handling code for the button. The event handling code for the button is as under.
protected void button1_Click (object sender, System.EventArgs e)
{
//Check if the TextBox has some
text
if(textBox1.Text!=null)
{
//Clear the
ListBox
listBox1.Items.Clear();
//Create a Instance of the Autogenerated Proxy
class
localhost.DailyStock ds
= new localhost.DailyStock();
//Call the WebMethod GetQuote and
pass
//the symbol from the
TextBox
string results =
ds.GetQuote(textBox1.Text);
// The returned string has values which are separated
// by
commas.
// Hence we split the
returned string into parts
char[] splitter = {','} ;
string[] temp = results.Split(splitter);
// Check if the string array returned has more than
15
// elements since if there
are less than 15 elements
//
then an exception must have been returned
if(temp.Length>15)
{
//Put all the values in the
ListBox
//The order of this is based on the order of Items
//returned by Yahoo's service
listBox1.InsertItem(0,"Stock Quotes Web Service from
.NET101");
listBox1.InsertItem(1,"Symbol
:"+temp[0]);
listBox1.InsertItem(2,"Current Index
:"+temp[1]);
listBox1.InsertItem(3,"Date/Time
:"+temp[2]+":"+temp[3]);
listBox1.InsertItem(4,"Change
:"+temp[4]);
listBox1.InsertItem(5,"Open
:"+temp[5]);
listBox1.InsertItem(6,"High
:"+temp[6]);
listBox1.InsertItem(7,"Low
:"+temp[7]);
listBox1.InsertItem(8,"Volume
:"+temp[8]);
listBox1.InsertItem(9,"Market Capitalization
:"+temp[9]);
listBox1.InsertItem(10,"Previous Close
:"+temp[10]);
listBox1.InsertItem(11,"Percentage Change
:"+temp[11]);
listBox1.InsertItem(12,"Range
:"+temp[13]);
listBox1.InsertItem(13,"Earning Per Share
:"+temp[14]);
listBox1.InsertItem(14,"Price Earning Ratio
:"+temp[15]);
listBox1.InsertItem(15,"Company Name
:"+temp[16]);
}
else
{
//This is a error message
listBox1.InsertItem(0,"Error: Getting
Stock-Quote");
}
}
}
Compile the Project
Save the project and
select Build Menu-> Build to compile the Project. Once compiled press Ctrl+F5
to run the Win Form Application. This completes the Win Form Consumer for our
Stock Quotes Web Service.
Figure 5
- 1一波“三折”:我的OA选型经历(上)
- 2Web Service Case Study:软件反馈跟踪平台
- 3Web服务内幕,第1部分:我们已走了多远?
- 4柴油机故障诊断专家系统知识库设计
- 5Web服务设计师,第1部分:动态电子商务介绍
- 6石家庄OA信息化的价值和挑战
- 7石家庄OA信息化的基本XML和RDF 技术(五):定义RDF和DAML+OIL图示
- 8石家庄OA知识归档管理与快速查询管理
- 9架构Web Service:什么是Web服务?
- 10微软等筹备Web服务盛会 Sun再遭冷遇
- 11源天软件获2008年度中国IT服务创新奖
- 12加速战略学习
- 13构建石家庄OA信息化系统的探讨
- 14OA网络办公系统信息传递的安全解决方案
- 15SOAP技术与B2B应用集成--SOAP消息中的类型/值的编序方法和示例
- 16使用Google的Web Service
- 17由知识螺旋看知识创新(BY AMT 夏敬华 编译)
- 18鄂尔多斯投资控股集团智能协同OA移动办公系统介绍
- 19石家庄OA信息化,知识组织和知识工作者:来自前沿的观点
- 20使用Visual Studio.Net建立web service
- 21亚马逊学习eBay推网络服务计划 免费为他人推广
- 222008协同软件冰火之年:概念褪去 普及延伸
- 23一波“三折”:我的OA选型经历(下)
- 24Web服务内幕,第7部分:WSFL和递归组合
- 25OA研发团队的项目管理主要关注9类关键活动,如下:
- 26网络服务热火如潮 前景扑朔迷离
- 27全球性学习型组织的十一个特征
- 28Web服务:WS-Inspection 1.0
- 29走出石家庄OA信息化的迷思(BY AMT 夏敬华)
- 30如何帮助企业构建安全的协同办公系统?
成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼