Export (0) Print
Expand All

Sending SOAP Requests by Using Visual Studio .NET Client (C#)

SQL Server 2000

This topic provides a sample application in which a Microsoft® Visual Studio® .NET client sends SOAP requests to a server computer that is running Microsoft SQLXML and processes the SOAP response accordingly. It is assumed that you have reviewed the following topics, which provide the relevant conceptual information:

The client application provides a dialog box in which you enter a CustomerID value. The client uses this value to send SOAP requests to execute the WSDL operations (methods). The results of these operations are returned as specified in the configuration of the operation:

  • The WSDL operation GetCustInfoAsXMLElementObjects returns output as XMLElement type objects and the errors as SQLMessage type objects in the object array.

  • The WSDL operation GetCustInfoAsDataSetObjects returns output as DataSet type objects and the error as an SQLMessage type object in the object array.

  • The WSDL operation GetCustAndOrderInfoAsADataSet returns output as a single DataSet object.

  • The WSDL operation UDFReturningATable operation returns output as a single DataSet object.

  • The WSDL operation SampleTemplate operation returns output as an XMLElement object and errors as SQLMessage objects in the object array.

When the results are returned as an object array, the application identifies the type of each object in the object array and then processes the object accordingly (assigns the object to a variable of the type identified and then displays it the list box).

To simplify the applications, the returned results are displayed on screen.

To create a working sample

  1. On the client computer, from the Microsoft Visual Studio .NET program group, start Microsoft Visual Studio .NET.

  2. Click New Project.

  3. Select Visual C# Projects as the Project Type.

  4. Specify the project name and the location where you want to save the project.

  5. Select Windows Application as the template, and then click OK.

  6. Right-click References, and then select Add Web Reference.

  7. In the Address box, type http://Server/nwind2/soap?wsdl, and then press Enter. (This brings the WSDL file to the screen.) Note that you must enter your server name in the URL.

  8. Click Add Reference.

  9. In the Form1, add a text box (textBox1), a button (button1), and a list box (listBox1).

  10. Right-click text box, and then select Properties. Change the Text value from textBox1 to ALFKI. This is the default Customer ID value.

  11. Right-click button1, and then select Properties. Change the Text property value from button1 to Execute SP and Template, and change the (name) property to ExecSPandTemplate.

  12. Right-click on the list box (listBox1), and then select Properties. Change the HorizontalScrollbar property to True.

  13. Double-click Execute SP and Template.

  14. Add the following code to the function. Upon completion of this task, the event appears as follows:

    In the code, WebReference.soap refers to the Web Reference and to the service that you add. You will need to change this in the code to the names that you specify when you add the Web reference.

            private void ExecSPandTemplate_Click(object sender, System.EventArgs e)
            {        
                string outParam = "";
                int returnValue = 0;
                WebReference.soap proxy = new WebReference.soap();
                //proxy.Credentials=System.Net.CredentialCache.DefaultCredentials;
                listBox1.Items.Add("1) Executing SP. Result returned as XMLElement objects...this is the result");
                listBox1.Items.Add("");
                
                // call sp GetCustomerInfo
                object[] results;
                    results = proxy.GetCustInfoAsXMLElementObjects(
                        textBox1.Text, ref outParam);
    
                for (int j=0; j<results.Length; j++)
                {
                    object e1;
                    localhost1.SqlMessage errorMessage;
                    System.Xml.XmlElement result;
                    e1= results[j];
                    // return value from the SP is an int
                    if (e1.GetType().IsPrimitive)
                    {
                        listBox1.Items.Add("Return code = ");
                        listBox1.Items.Add(e1);
                    }
                    switch (e1.ToString())
                    {
                        case "System.Xml.XmlElement":
                            result = (System.Xml.XmlElement) results[j];
                            listBox1.Items.Add(result.OuterXml);
                            break;
    
                        case "CSharpSQLXML.localhost1.SqlMessage":
                            errorMessage = (localhost1.SqlMessage) results[j];
                            listBox1.Items.Add(errorMessage.Message);
                            listBox1.Items.Add(errorMessage.Source);
                            break;
                    }
                }
                // call sp GetCustomerInfo again
                listBox1.Items.Add("");
                listBox1.Items.Add("2) Executing SP. Result returned as DataSet objects...this is the result");
                listBox1.Items.Add("");
                object[] results3;
                results3 = proxy.GetCustInfoAsDataSetObjects(textBox1.Text, ref outParam);
    
                for (int j=0; j<results3.Length; j++)
                {
                    object e1;
                    localhost1.SqlMessage errorMessage;
                    System.Data.DataSet resultDS;
                    e1= results3[j];
                    // return value from the SP is an int
                    if (e1.GetType().IsPrimitive)
                    {
                        listBox1.Items.Add("Return code = ");
                        listBox1.Items.Add(e1);
                    }
                    switch (e1.ToString())
                    {
                        case "System.Data.DataSet":
                            resultDS = (System.Data.DataSet) results3[j];
                            listBox1.Items.Add(resultDS.GetXml());
                            break;
                        case "CSharpSQLXML.localhost1.SqlMessage":
                            errorMessage = (localhost1.SqlMessage) results3[j];
                            listBox1.Items.Add(errorMessage.Message);
                            listBox1.Items.Add(errorMessage.Source);
                            break;
                    }
               }
                // calling sp to return a dataset
                listBox1.Items.Add("");
                listBox1.Items.Add("3) Executing SP. Result returned as a single DataSet ...this is the result");
                listBox1.Items.Add("");
                System.Data.DataSet results4 = 
                    proxy.GetCustAndOrderInfoAsADataSet(
                        textBox1.Text, ref outParam, out returnValue);
                listBox1.Items.Add(results4.GetXml());
                listBox1.Items.Add("Return value: ");
                listBox1.Items.Add(returnValue);
    
                // executing a UDF
                listBox1.Items.Add("");
                listBox1.Items.Add("4) Executing UDF. Resulting table returned as a single DataSet ...this is the result");
                listBox1.Items.Add("");
                System.Data.DataSet results5 = 
                    proxy.UDFReturningATable(textBox1.Text);
                listBox1.Items.Add(results5.GetXml());
        
    
                // end of UDF
                listBox1.Items.Add("");
                listBox1.Items.Add("5) Executing template. Result returned as XMLElement objects ...this is the result");
                listBox1.Items.Add(""); 
                // call template
                object[] results2;
    
                    results2 = proxy.SampleTemplate(textBox1.Text);
            for (int j=0; j<results2.Length; j++)
                {
                    object e1;
                    localhost1.SqlMessage errorMessage;
                    System.Xml.XmlElement result;
                    System.Xml.XmlElement el = (System.Xml.XmlElement) results2[j];
                    e1= results2[j];
                    switch (e1.ToString())
                    {
                        case "System.Xml.XmlElement":
                            result = (System.Xml.XmlElement) results2[j];
                            listBox1.Items.Add(el.OuterXml);
                            break;
                        case "SqlMessage":
                            errorMessage = (localhost1.SqlMessage)results2[j];
                            listBox1.Items.Add(el.OuterXml);
                            break;
                    }
                }
            }
    

    Note that this sample assumes that you specified SQL Server login information when you created the nwind2 virtual directory. If you specified Windows Integrated Security or Basic Authentication instead, you will need to add code, as follows:

    In the case of Windows Integrated Authentication, add:

    proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    

    In the case of Basic Authentication, add:

    proxy.Credentials = new System.Net.NetworkCredential(UserID, UserPassword);
    
  15. Save the form, and execute it. Enter the CustomerID value in the text box (for example, "ALFKI"), and then click ExecSPandTemplate. After you click Execute SP and Template, a SOAP request is sent to execute the sample stored procedures, the user-defined function, and the sample template. The results are displayed in the list box.

To return a SOAP fault in case of an error, modify the configuration of the soap virtual name and select the Return errors as soap fault option for the stored procedure and template. This process re-creates the WSDL file. Therefore, you must update the Web reference in your application to use the new WSDL file and then execute the application again.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft