This sample HTML form prompts a user to enter an employee ID, first name, and last name. After the user has entered the data in the form, an XML document containing the employee element to be inserted in the database is created. The XML document is passed as a parameter to the template.
Before executing this example, you must create a virtual root. For more information, see Creating the nwind Virtual Directory.
This example shows:
The template executes a stored procedure. The XML document is passed to the stored procedure as a text parameter.
The stored procedure:
These are the steps to create a working sample:
CREATE TABLE Employee(eid int, fname varchar(20), lname varchar(20))
CREATE PROC sp_insert_employee @empdata ntext
AS
DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @empdata
INSERT INTO Employee
SELECT *
FROM OPENXML(@hDoc, '/Employee')
WITH Employee
EXEC sp_xml_removedocument @hDoc
<root xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<sql:header>
<sql:param name="empdata"><Employee/></sql:param>
</sql:header>
<sql:query>exec sp_insert_employee @empdata
</sql:query>
</root>
This is the HTML form:
<html>
<body>
<form action="http://IISServer/nwind/template
/MyTemplate.xml" method="post">
<input type="hidden" id="e" name="empdata">
<input type="hidden" name="contenttype" value="text/xml">
EmployeeID: <input type=text id=eid value="1"><br>
First Name: <input type=text id=fname value="Harry"><br>
Last Name: <input type=text id=lname value="Smith"><br>
<input type=submit onclick="Insert_Employee(e, eid,
lname, fname)" value="Insert Employee"><br><br>
<script>
function Insert_Employee(e, eid, lname, fname)
{
e.value = '<Employee eid="' + eid.value +
'" lname="' + lname.value + '" fname="' +
fname.value + '"/>';
}
</script>
</form>
</body>
</html>