Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a simple web service that accepts a stored procedure name and a string of data. It uses those values to create a connection to a database and executes said stored procedure that is passed the string value from the ASP page. I am trying to return the data from the stored procedure as XML back to the ASP page and display it.

I will post the code I have that is attempting to do this. I would appreciate any guidance or suggestions on what to do, thank you.

This is the VBScript I have on my ASP page that gets the name/value pairs from three input fields and sends the info via SOAP to the service.
VB
Dim soapServer, soapMessage, data

    For x = 1 To Request.Form.Count()
        fieldName = Request.Form.Key(x)
        fieldValue = Request.Form.Item(x)
        If fieldName <> "Submit" And fieldName <> "Password" Then
            data = data & fieldName & "~" & fieldValue & "|"
        End If
    Next

    'service location - Production
    soapServer = "http://intranet.domain.com:54321/DataExchangeService/DataExchangeService.svc"
     
    'message including soap envelope wrapper, to send to the service
    soapMessage = "<s:Envelope xmlns:s=" & Chr(34) & "http://schemas.xmlsoap.org/soap/envelope/" & Chr(34) & ">" & _
                        "<s:Body>" & _
                            "<DataExchange xmlns=" & Chr(34) & "http://tempuri.org/" & Chr(34) & ">" & _
                                "<storedProcedure>" & storedProcedure & "</storedProcedure>"  & _
                                "<data>" & data & "</data>" & _
                            "</DataExchange>" & _
                        "</s:Body>" & _
                    "</s:Envelope>"

    Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")

    xmlhttp.open "POST", soapServer, False
    xmlhttp.setRequestHeader "Man", POST & " " & soapServer & " HTTP/1.1"
    xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/IDataExchangeService/DataExchange"
    xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"

    'Calling the service'
    xmlhttp.send(soapMessage)

 Response.Write xmlhttp.responseText


This is the c# code in my actual service.
public class DataExchangeService : IDataExchangeService
    {
        //The code below handles the customer match checking process
        public string DataExchange(string storedProcedure, string data)
        {
            string connString = ConfigurationManager.ConnectionStrings["DataExchangeConnString"].ConnectionString;
            string returnData = "";

            SqlParameter dataParameter = new SqlParameter();
            dataParameter.Value = data;
            dataParameter.ParameterName = "@data";

            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(storedProcedure, conn);
            SqlDataReader reader = null;

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(dataParameter);
            
            try
            {
                conn.Open();
                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    returnData = SerializeToXml(reader);
                }
                return returnData;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                reader.Close();
                conn.Close();
            }
        }//end DataExchange

        public string SerializeToXml(object value)
        {
            StringWriter writer = new StringWriter();
            XmlSerializer serializer = new XmlSerializer(value.GetType());
            serializer.Serialize(writer, value);
            return writer.ToString();
        }
    }//end dataexchangeservice
}//end namespace


Nothing errors out, it just does not display anything on the page other than the HTML that was already there.
Posted
Updated 13-Dec-12 7:00am
v4

I figured out my problem. I was not creating a DOM Document in my VBScript to hold my xml response. Also, my serializing of my data from the stored procedure needed some tweaking too.

Here is what I added:

This is my VBScript.
VB
Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false

Response.Write(xmlhttp.ResponseXml.xml)



This is my code-behind.
DataTable dataTable = new DataTable();
dataTable.TableName = "ReturnData";
dataTable.Load(reader);
dataTable.WriteXml(xmlWriter, XmlWriteMode.WriteSchema, false);
 
Share this answer
 
v2
Comments
Tarun Mangukiya 18-Dec-12 2:18am    
Yes!
I think that DOM is correct solution. I am using...
You would do better to write web pages that work in browsers other than IE. You could also then use Chrome to debug your code. You can also add MsgBoxes to your script to view the return from your function call, to see what is coming back.
 
Share this answer
 
Comments
Richard C Bishop 13-Dec-12 17:58pm    
That is a good point. So are you saying that the VBScript would not work in Chrome? I have the service code published to a test server and could access my ASP page in any browser. How do I go about debugging in the Chrome browser?
Christian Graus 13-Dec-12 18:00pm    
Wow - you got this far and didn't know that ? Yes, VBScript is IE only. You should be testing in Chrome NOW, and so should know this. In Chrome, if you right click on an element and choose 'Inspect Element', a debugger opens where you can set breakpoints in script, etc
Richard C Bishop 13-Dec-12 18:07pm    
Well this is my first time ever using VBScript so I still have a lot to learn. Thank you for the info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900