Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP XML
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.
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 13-Dec-12 5:23am
Edited 13-Dec-12 7:00am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
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);
  Permalink  
v2
Comments
Tarun Y Mangukiya at 18-Dec-12 2:18am
   
Yes!
I think that DOM is correct solution. I am using...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
richcb at 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 at 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
richcb at 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)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 354
3 PIEBALDconsult 205
4 BillWoodruff 190


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100