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.

Prérequis

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.

Contexte et remarques

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.

 Task
  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;
    
    listBox1.Items.Clear();
    
    localhost.SASoapTest_DNET proxy = new localhost.SASoapTest_DNET();
    DataSet results = proxy.EmployeeList(out sqlCode);
    DataTableReader dr = results.CreateDataReader();
    
    listBox1.BeginUpdate();
    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)");
            }
            else
            {
                switch (typeCode)
                {
                    case System.TypeCode.Int32:
                        Int32 intValue = dr.GetInt32(i);
                        listBox1.Items.Add(columnName + intValue);
                        break;
                    case System.TypeCode.Decimal:
                        Decimal decValue = dr.GetDecimal(i);
                        listBox1.Items.Add(columnName + decValue.ToString("c"));
                        break;
                    case System.TypeCode.String:
                        string stringValue = dr.GetString(i);
                        listBox1.Items.Add(columnName + stringValue);
                        break;
                    case System.TypeCode.DateTime:
                        DateTime dateValue = dr.GetDateTime(i);
                        listBox1.Items.Add(columnName + dateValue);
                        break;
                    case System.TypeCode.Boolean:
                        Boolean boolValue = dr.GetBoolean(i);
                        listBox1.Items.Add(columnName + boolValue);
                        break;
                    default:
                        listBox1.Items.Add(columnName + "(unsupported)");
                        break;
                }
            }
        }
        listBox1.Items.Add("");
    }
    listBox1.EndUpdate();
    dr.Close();
  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:



    (Int32)EmployeeID=102
    (Int32)ManagerID=501
    (String)Surname=Whitney
    (String)GivenName=Fran
    (Int32)DepartmentID=100
    (String)Street=9 East Washington Street
    (String)City=Cornwall
    (String)State=NY
    (String)Country=USA
    (String)PostalCode=02192
    (String)Phone=6175553985
    (String)Status=A
    (String)SocialSecurityNumber=017349033
    (Decimal)Salary=$45,700.00
    (DateTime)StartDate=8/28/1984 12:00:00 AM
    (DateTime)TerminationDate=(null)
    (DateTime)BirthDate=6/5/1958 12:00:00 AM
    (Boolean)BenefitHealthInsurance=True
    (Boolean)BenefitLifeInsurance=True
    (Boolean)BenefitDayCare=False
    (String)Sex=F

    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.

Résultat

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:



<row>
 <EmployeeID>102</EmployeeID>
 <ManagerID>501</ManagerID>
 <Surname>Whitney</Surname>
 <GivenName>Fran</GivenName>
 <DepartmentID>100</DepartmentID>
 <Street>9 East Washington Street</Street>
 <City>Cornwall</City>
 <State>NY</State>
 <Country>USA</Country>
 <PostalCode>02192</PostalCode>
 <Phone>6175553985</Phone>
 <Status>A</Status>
 <SocialSecurityNumber>017349033</SocialSecurityNumber>
 <Salary>45700.000</Salary>
 <StartDate>1984-08-28</StartDate>
 <BirthDate>1958-06-05</BirthDate>
 <BenefitHealthInsurance>1</BenefitHealthInsurance>
 <BenefitLifeInsurance>1</BenefitLifeInsurance>
 <BenefitDayCare>0</BenefitDayCare>
 <Sex>F</Sex>
 </row>

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:

<ADate>2013-01-27</ADate>
<ATime>12:34:56.000-08:00</ATime>
<ADateTime>2013-01-27T12:34:56.000-08:00</ADateTime>
<ADateTimeWithZone>2013-01-27T12:34:56.000+06:00</ADateTimeWithZone>

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' />