Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - Programming » HTTP web services » HTTP web service examples » Tutorial: Using Visual C# to access a SOAP/DISH web service


Lesson 2: Creating a Visual C# application to communicate with the web server

In this lesson, you create a Visual C# application to communicate with the web server.


This lesson assumes that you have set up a web server as instructed in lesson 1. See Lesson 1: Setting up a web server to receive SOAP requests and send SOAP responses.

A recent version of Visual Studio is required to complete this lesson.

This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Using Visual C# to access a SOAP/DISH web service.

Context and remarks

This lesson contains several references to localhost. Use the host name or IP address of the web server from lesson 1 instead of localhost if you are not running the web client on the same computer as the web server.

This example uses functions from the .NET Framework 2.0.

  1. Start Visual Studio.

  2. Create a new Visual C# Windows Forms Application project.

    An empty form appears.

  3. Add a web reference to the project.

    1. Click Project » Add Service Reference.

    2. In the Add Service Reference window, click Advanced.

    3. In the Service Reference Settings window, click Add Web Reference.

    4. In the Add Web Reference window, type http://localhost:8082/demo/SASoapTest_DNET in the URL field.

    5. Click Go (or the green arrow).

      Visual Studio lists the EmployeeList method available from the SASoapTest_DNET service.

    6. Click Add Reference.

      Visual Studio adds localhost to the project Web References in the Solution Explorer pane.

  4. Populate the empty form with the desired objects for web client application.

    From the Toolbox pane, drag ListBox and Button objects onto the form and update the text attributes so that the form looks similar to the following diagram:

    SOAP demonstration form.
  5. Write a procedure that accesses the web reference and uses the available methods.

    Double-click the Employee List button and add the following code for the button click event:

    int sqlCode;
    localhost.SASoapTest_DNET proxy = new localhost.SASoapTest_DNET();
    DataSet results = proxy.EmployeeList(out sqlCode);
    DataTableReader dr = results.CreateDataReader();
    while (dr.Read())
        for (int i = 0; i < dr.FieldCount; i++)
            string dataTypeName = dr.GetDataTypeName(i);
            string dataName = dr.GetName(i);
            System.Type ftype = dr.GetFieldType(i);
            System.TypeCode typeCode = System.Type.GetTypeCode(ftype);
            string columnName = "(" + dataTypeName + ")" + dataName + "=";
            if (dr.IsDBNull(i))
                listBox1.Items.Add(columnName + "(null)");
                switch (typeCode)
                    case System.TypeCode.Int32:
                        Int32 intValue = dr.GetInt32(i);
                        listBox1.Items.Add(columnName + intValue);
                    case System.TypeCode.Decimal:
                        Decimal decValue = dr.GetDecimal(i);
                        listBox1.Items.Add(columnName + decValue.ToString("c"));
                    case System.TypeCode.String:
                        string stringValue = dr.GetString(i);
                        listBox1.Items.Add(columnName + stringValue);
                    case System.TypeCode.DateTime:
                        DateTime dateValue = dr.GetDateTime(i);
                        listBox1.Items.Add(columnName + dateValue);
                    case System.TypeCode.Boolean:
                        Boolean boolValue = dr.GetBoolean(i);
                        listBox1.Items.Add(columnName + boolValue);
                        listBox1.Items.Add(columnName + "(unsupported)");
  6. Run the application.

    Click Debug » Start Debugging.

  7. Communicate with the web database server.

    Click Employee List.

    The ListBox object displays the EmployeeList result set as (type)name=value pairs. The following output illustrates how an entry appears in the ListBox object:

    (String)Street=9 East Washington Street
    (DateTime)StartDate=8/28/1984 12:00:00 AM
    (DateTime)BirthDate=6/5/1958 12:00:00 AM

    The Salary amount is converted to the client's currency format.

    Values that contain a date with no time are assigned a time of 00:00:00 or midnight (which is displayed in a format dependent on the client's locale settings).

    Values that contain null are tested using the DataTableReader.IsDBNull method.


The XML response from the web server includes a formatted result set. All column data is converted to a string representation of the data. The following result set illustrates how result sets are formatted when they are sent to the client:

 <Street>9 East Washington Street</Street>

The DATATYPE ON clause was specified in the previous lesson to generate data type information in the XML result set response. A fragment of the response from the web server is shown below. The type information matches the data type of the database columns.

<xsd:element name='EmployeeID' minOccurs='0' type='xsd:int' />
<xsd:element name='ManagerID' minOccurs='0' type='xsd:int' />
<xsd:element name='Surname' minOccurs='0' type='xsd:string' />
<xsd:element name='GivenName' minOccurs='0' type='xsd:string' />
<xsd:element name='DepartmentID' minOccurs='0' type='xsd:int' />
<xsd:element name='Street' minOccurs='0' type='xsd:string' />
<xsd:element name='City' minOccurs='0' type='xsd:string' />
<xsd:element name='State' minOccurs='0' type='xsd:string' />
<xsd:element name='Country' minOccurs='0' type='xsd:string' />
<xsd:element name='PostalCode' minOccurs='0' type='xsd:string' />
<xsd:element name='Phone' minOccurs='0' type='xsd:string' />
<xsd:element name='Status' minOccurs='0' type='xsd:string' />
<xsd:element name='SocialSecurityNumber' minOccurs='0' type='xsd:string' />
<xsd:element name='Salary' minOccurs='0' type='xsd:decimal' />
<xsd:element name='StartDate' minOccurs='0' type='xsd:date' />
<xsd:element name='TerminationDate' minOccurs='0' type='xsd:date' />
<xsd:element name='BirthDate' minOccurs='0' type='xsd:date' />
<xsd:element name='BenefitHealthInsurance' minOccurs='0' type='xsd:boolean' />
<xsd:element name='BenefitLifeInsurance' minOccurs='0' type='xsd:boolean' />
<xsd:element name='BenefitDayCare' minOccurs='0' type='xsd:boolean' />
<xsd:element name='Sex' minOccurs='0' type='xsd:string' />

Columns containing only the date are formatted as yyyy-mm-dd in the XML response.

Columns containing only the time are formatted as hh:mm:ss.nnn-HH:MM or hh:mm:ss.nnn+HH:MM in the XML response. A zone offset (-HH:MM or +HH:MM) is suffixed to the string.

Columns containing both date and time are formatted as yyyy-mm-ddThh:mm:ss.nnn-HH:MM or yyyy-mm-ddThh:mm:ss.nnn+HH:MM in the XML response. The date is separated from the time using the letter 'T'. A zone offset (-HH:MM or +HH:MM) is suffixed to the string.

Columns containing date, time, and time zone offset are formatted as yyyy-mm-ddThh:mm:ss.nnn-HH:MM or yyyy-mm-ddThh:mm:ss.nnn+HH:MM in the XML response. The date is separated from the time using the letter 'T'. A zone offset (-HH:MM or +HH:MM) is suffixed to the string.

Some examples for a server running in the Pacific time zone follow:


The format in which these dates and times are displayed in a .NET application depends on the client's time zone and locale settings. The following is an example for a client in the Eastern time zone with a United States locale (assuming that today's date is January 28, 2013).

(DateTime)ADate=1/27/2013 12:00:00 AM 
(DateTime)ATime=1/28/2013 3:34:56 PM
(DateTime)ADateTime=1/27/2013 3:34:56 PM
(DateTime)ADateTimeWithZone=1/27/2013 1:34:56 AM

Note that there is no separate Date and Time in the TypeCode enumeration - there is only TypeCode.DateTime - which explains why all results include both a date and time.

The data type information in the XML response for these dates and times follows:

<xsd:element name='ADate' minOccurs='0' type='xsd:date' />
<xsd:element name='ATime' minOccurs='0' type='xsd:time' />
<xsd:element name='ADateTime' minOccurs='0' type='xsd:dateTime' />
<xsd:element name='ADateTimeWithZone' minOccurs='0' type='xsd:dateTime' />