Introduction
In the previous part, we made a simple WebService
, that allows storing and retrieving simple messages. In this step, we make a simple stand-alone Windows Forms client, for this service.
Part II
Client-Side
Step 1: Skeleton Project
First, we make a skeleton project for the client app. This is a simple Windows Application project with two Form
delivered classes:
ViewForm
- form for viewing posted messages NewMessageForm
- form for posting a new message
ViewForm
NewMessageForm
NewMessageForm
creating can be completed with Forms designer, in three steps:
- setting
DialogResult
property for 'Post' button to OK
- setting
DialogResult
property for 'Cancel' button to Cancel
- setting
Midifiers
property for TextBox
to public
ViewForm
class demands more complex code.
Step 2: Reading Data from Service
The next step is to read posted messages from the webservice
. In this step of developing, I added this code to the ButtonClick
event of 'Update
' button. In future, this code will be called for complex tasks.
private void BtnUpdate_Click(object sender, System.EventArgs e)
{
XmlDocument xdoc = new XmlDocument();
msgView.Items.Clear();
try
{
xdoc.Load("http://dbserv:88/todo/log.asmx/GetLog?");
XmlNodeList nodes = xdoc.SelectNodes("/log/message");
foreach (XmlNode node in nodes)
{
try
{
ListViewItem item =
new ListViewItem(node.Attributes["posted"].InnerText);
item.SubItems.Add(node.InnerText);
msgView.Items.Add(item);
}
catch (Exception)
{
ListViewItem item = new ListViewItem("Corrupted!!!");
msgView.Items.Add(item);
}
}
}
catch (Exception ex)
{
ListViewItem item = new ListViewItem("Failed");
item.SubItems.Add(ex.Message);
msgView.Items.Add(item);
}
}
First two lines of function code initialize empty XmlDocument
instance and clear all stored items from ListView
control.
In the first try
...catch
block, load stored messages from server (XmlDocument.Load()
) and retrieve list of nodes containing messages' data (XmlDocument.SelectNodes()
). foreach
block browses items in result collection of XmlNode
objects. Nested try
...catch
block creates new ListViewItem
object for each message, parses XmlNode
object for message data, and stores it in ListViewItem
object.
Step 3: Post New Messages to Service
The next step is to implement the ability to post new messages from client to server. Code for this is placed in ButtonClick
event handler for button 'Post'.
private void NewPost_Click(object sender, System.EventArgs e)
{
NewMessageForm dlg = new NewMessageForm();
if (dlg.ShowDialog() == DialogResult.OK)
{
System.Collections.Specialized.NameValueCollection parms =
new System.Collections.Specialized.NameValueCollection(1);
parms.Add("message", dlg.msgText.Text);
WebClient req = new WebClient();
req.UploadValues("http://dbserv:88/todo/log.asmx/PostMessage", parms);
}
dlg.Dispose();
}
If you have some questions about this function, post it in the comments section of this article (for me, it is very simple code).
Using the Code
Install and Use Code
This is all for this step. In result, we have a simple Stand-Alone client program that allows us to interact with the server.
To test this code, download the demo project. Change 'http://dbserv:88/togo/' in source to your WebApp home URL and build solution.
Copy code library for WEB application located in web_bin directory to bin directory of your Web application. Create log.asmx file in root folder of your web application. log.asmx file must contain only this row:
<%@ WebService language="C#" class="devel.Log" %>
Client program is located in the bin folder.
History
Part I of this article can be located here.
P. S.
Post in comments/questions about code parts. I plan to update article description based on your questions.
License
This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.
A list of licenses authors might use can be found here.