Tutorials, Downloads, and Sample Applications    SQLAjax Whitepaper    Related Links        Contact Us          

AJAX Simplified: Passing XML to a SQL stored procedure

Examine the following simple SQLAjax stored procedure that simply returns as a large string whatever XML is passed to it:

     -- =============================================
    -- Author:	SQLAjax
    -- Create date: March 2006
    -- Description:	Tutorial/Utility procedure simply returns as a string
    --				whatever XML is passed in. This demonstrates
    --				the key SQLAjax concept of 
    --				"one string in - one string out"
    -- =============================================
    CREATE PROCEDURE xreflection 
	    @xmlin xml = null
    AS
    SELECT @xmlin

Click the button below to send a simple AJAX message to the xreflection stored procedure and see the XML returned as text in a javascript alert window.

Notice how quickly the XML you sent was returned back to you through the SQLAjax framework. There is simply no faster way to move data between a web page and a SQL database system. The code below shows the javascript functions from this page that send the XML message to the SQL database system and receive the response.


    function xreflection()
    {
        // build an XML message that describes a procedure and its parameters
        var strMessage = '<Message><MessageType>xreflection</MessageType>'
            + '<SomeText>SQLAjax makes web programming much easier</SomeText></Message>';

        // transmit the message to the SQLAjax generic handler on the web server
        // The second parameter for MessageSend is a pointer to the javascript callback 
        // procedure that will execute and process the response from the SQL database server
        MessageSend("xSQLAjax.ashx", strMessage, xreflectionReceive)
    }
    
    function xreflectionReceive()
    {
        // the browser received a string from the xSQLAjax.ashx generic handler
        // addressed to this procedure. 
        // get a pointer to the string that is being sent from the database.
        var strFromSQL = ajaxRequest.getXMLHttpRequestObject().responseText;
        alert(strFromSQL);
    }
    

The two functions above are what is known as an "asynchronous callback pair." This type of framework is what makes AJAX "asynchronous," and means that the calling procedure does not "block" execution of subsequent javascript code running in the page. Instead, the "callback procedure" is called by the BROWSER when a response to the AJAX message is received from the web server (and xSQLAjax.ashx in this case). In most cases, users will be "waiting" for the response from the database server, but the response will always be as fast as your stored procedure, because there is no additional code to execute in the "middle-tier." This is why SQLAjax is by-far the fastest web development platform available for HTTP.

The following shows the stack that your message passed through:

1. xreflectionSend() called MessageSend() in the xAjaxClass.js opensource wrapper file. (to download separately, visit taconite.sourceforge.net.)

2. MessageSend() passed the XML tree to the ProcessRequest() function in the xSQLAjax.ashx generic handler.

3. The xSQLAjax.ashx generic handler passed the XML string to the stored procedure xsqlajax using a few lines of code and the ADO.NET ExecuteScalar() method..

4. The stored procedure xsqlajax, acting as a dispatcher or gateway, passed the XML string the stored procedure xreflection.

5. The stored procedure xreflection returned a string to the xSQLAjax.ashx generic handler

6. The xSQLAjax.ashx generic handler returned the string to xreflectionReceive() javascript function in this web page.

7. The xreflectionReceive() javascript function in this web page showed the string sent back from xreflection, which was simply the same XML string we passed out.

Proceed to Tutorial #2 - SQLAjax Security