
Introduction
In 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
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("Corupted!!!");
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 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
Next step is to implement 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.