監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設(shè)計管理系統(tǒng) | 甲方項目管理系統(tǒng) | 簽約案例 | 客戶案例 | 在線試用
X 關(guān)閉

Consuming a Web Service from a Win Form Application

申請免費試用、咨詢電話:400-8352-114

AMTeam.org

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

發(fā)布:2007-03-25 13:24    編輯:泛普軟件 · xiaona    [打印此頁]    [關(guān)閉]
相關(guān)文章:
石家莊OA系統(tǒng)
聯(lián)系方式

成都公司:成都市成華區(qū)建設(shè)南路160號1層9號

重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓

咨詢:400-8352-114

加微信,免費獲取試用系統(tǒng)

QQ在線咨詢