Quick Start

WebGrid EXP Files

WebGrid EXP files include :

Besides those files above, we also include supporting files :

Attaching WebGrid EXP on an HTML page

First, you need to include javascript & stylesheet file of WebGrid EXP using the following code :

<link rel="stylesheet" type="text/css" href="include/WebGridEXP.css">
<script language="JavaScript" src="include/WebGridEXP.js"></script>

Then, we can attach the grid using the code below (note that the parameter has to be filled with the variable name) :

<script>
var obj1 = new WebGridEXP("obj1")
</script>

The next important thing is to set the WebGrid EXP properties according to your needs. Below is a sample of HTML file that is using WebGrid EXP : (We will refer to this HTML file as : sample1.htm)

<html>
<head>
	<link rel="stylesheet" type="text/css" href="include/WebGridEXP.css">
	<script language="JavaScript" src="include/WebGridEXP.js"></script>
	<script language="JavaScript">
	function Init()
		{
		obj1.COLUMNS    =	["TestID",
				"CaseDesc",
				"TestResult",
				"Comment",
				"NeedToFix"]
									
		obj1.IDENTITY   =	["TestID"]
							
		obj1.INPUTTYPES =	[Array("Text",255,false,false),
				Array("Text",255,true,true),
				Array("Text",255,true,true),
				Array("Text",255,true,true),
				Array("Boolean",true)]
							
		obj1.CAPTIONS   =	["ID",
				"Case Description",
				"Result",
				"Comment",
				"Need To Fix"]
							
		obj1.CELLWIDTH  =	[35,190,190,190,95]
		obj1.CELLHEIGHT =	100
				
		obj1.UrlGET     =	"dataGET/sampleGet1.asp" 
		obj1.UrlPOST    =	"dataPOST/samplePost1.asp" 
		obj1.ImgURL     =	"include"

		obj1.getData();
		}
	</script>	
</head>
<body onload="Init()">

<script>
var obj1 = new WebGridEXP("obj1")
</script>

</body>
</html>

As seen on the sample, we need to set several properties such as : COLUMNS, IDENTITY, INPUTTYPES, CAPTIONS, CELLWIDTH, CELLHEIGHT, UrlGET, UrlPOST & ImgURL. Then, we use method : getData() to display the data. Brief explanation of those properties is illustrated in the picture below :

Before we proceed with further explanation on WebGrid EXP properties and methods, we need to know first how WebGrid EXP works (complete references on WebGrid EXP properties and methods is available on WebGrid EXP References).

How WebGrid EXP works

In order to use WebGrid EXP, we need 2 server side pages which are used for :

  1. Getting Data from Database; Getting data from database and delivering the data to the client/WebGrid EXP.
  2. Updating Data to Database; Receiving the updated data from WebGrid EXP and applying the changes to the database.

The process of passing data between WebGrid EXP & the two server-side pages uses XML.

Here is the illustration :

To help implementing those two server-side pages, we have developed data access utility for getting and updating data from and to database :

Below is the explanation of using DataAccess.asp :

Getting XML Data From Database

First, we need to create blank ASP page. This ASP page will be used for Getting XML Data From Database. We will refer to this page as : SampleGet1.asp. We need to include the DataAccess.asp into this page using :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%>

After that, we can use clsQuery object to get the XML data from database. For this purpose, we need to specify the SQL Query, Database Connection, and some other properties related to Paging. To get the XML data we only need to call GetPage()method.

The complete code for sampleGet1.asp is as follows :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%> 
<%
Dim oQuery
Set oQuery = New clsQuery
oQuery.ConnectString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Server.MapPath("\database/grid.mdb")
oQuery.SQL = "SELECT TestID,CaseDesc,TestResult,Comment,NeedToFix FROM Test"
oQuery.PageSize	= Request.QueryString("PageSize")
oQuery.ShowPage	= Request.QueryString("ShowPage")
oQuery.OrderBy	= Request.QueryString("OrderBy")
oQuery.OrderType = Request.QueryString("OrderType")

Response.ContentType = "text/xml"
Response.Write "<?xml version='1.0' encoding='ISO-8859-1'?>" & vbCRLF 
Dim oDoc
set oDoc = oQuery.GetPage()
Response.Write oDoc.xml
%>

As seen on the above codes, the steps we need to do are :

Below is the sample of the XML data result :

Next, we can use the sampleGet1.asp together with sample1.htm (contains the WebGrid EXP). To specify the server page that we are going to use to get the XML data is as follows (see the sample1.htm illustration before) :

obj1.UrlGET     =	"dataGET/sampleGet1.asp" 

Posting XML Data To the Database

First, we need to create blank ASP page. This ASP page wil be used for Posting XML Data To the Database. We will refer to this file as : samplePost1.asp. We need to include DataAccess.asp into this page using :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%>

After that, we can instantiate the clsPost object. We need to specify the Database Connection, Table, Fields name, Primary Keys dan data types. To update the data back to database, we only need to call Post() method. Below is the sample of the implementation :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%> 
<%
Dim oPost
Set oPost = New clsPost
oPost.ConnectString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Server.MapPath("\database/grid.mdb")
oPost.TableName = "Test"
oPost.FieldList = Array("CaseDesc","TestResult","Comment","NeedToFix")
oPost.DataTypes = Array("TEXT","TEXT","TEXT","BOOLEAN")
oPost.PrimaryKeys = Array("TestID")
oPost.PKDataTypes = Array("NUMBER")

Dim oDocErr
set oDocErr = oPost.Post()
%>

As seen above, steps that we need to take are as follows :

Next, after the update operation is done, we need to re-fetch the data from database to be displayed on the grid as the most current data. We use the same technique as before, and adding error message information (from Post()) inside :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%>
<%
Dim oQuery
Set oQuery = New clsQuery
oQuery.ConnectString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Server.MapPath("\database/grid.mdb")
oQuery.SQL = "SELECT * FROM Test"
oQuery.PageSize	= Request.QueryString("PageSize")
oQuery.ShowPage	= Request.QueryString("ShowPage")
oQuery.OrderBy	= Request.QueryString("OrderBy")
oQuery.OrderType = Request.QueryString("OrderType")

Response.ContentType = "text/xml"
Response.Write "<?xml version='1.0' encoding='ISO-8859-1'?>" & vbCRLF 
Dim oDocResult
set oDocResult = oQuery.GetPage()

'Add Error Report
set oNode = oDocErr.documentElement
oDocResult.documentElement.appendChild(oNode) 
Response.Write oDocResult.xml
%>

The complete code for samplePost1.asp is as follows :


<%
Response.Buffer = True
Response.Expires = -1
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control", "no-store"

Class clsQuery
	
	Private sSQL
	Private sConnectString
	Private sPageSize
	Private sShowPage
	Private sOrderBy
	Private sOrderType
	Private arrExpression

	
	Public Property Let Expression(ByRef vVal)
		arrExpression = vVal
	End Property	

	Public Property Let SQL(ByRef vVal)
		sSQL = vVal
	End Property
	
	Public Property Let OrderBy(ByRef vVal)
		sOrderBy = vVal
	End Property	
	
	Public Property Let OrderType(ByRef vVal)
		if vVal = "" then 
			sOrderType = "ASC" 'default value
		else
			sOrderType = vVal
		end if		
	End Property
	
	Public Property Let ShowPage(ByRef vVal)
		if vVal = "" then 
			sShowPage = 1 'default value
		else
			sShowPage = CInt(vVal)
		end if
	End Property
	
	Public Property Let PageSize(ByRef vVal)
		if vVal = "" then 
			sPageSize = 10 'default value
		else
			sPageSize = CInt(vVal)
		end if
	End Property
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property

	Public Function GetPage()
		
		sNewOrderBy = ""
		
		If sOrderBy <> "" Then
		
			Dim Item
			NumOfExps = 0
			if not IsEmpty(arrExpression) then
				For Each Item In arrExpression
					NumOfExps = NumOfExps + 1
				Next
			end if

			if NumOfExps>0 then
				For i = 0 to CInt(NumOfExps)-1
						sAlias = arrExpression(i)(0)
						sExp = arrExpression(i)(1)
						sNewOrderBy = replace(sOrderBy,sAlias,sExp)
				Next
				sSQL = sSQL & " Order By " & sNewOrderBy & " " & sOrderType
			else
				sSQL = sSQL & " Order By " & sOrderBy & " " & sOrderType
			end if

		End If	

		Dim rs
		Set rs = server.CreateObject("ADODB.Recordset") 
		rs.CursorLocation = 3
		rs.Open sSQL, sConnectString, 3, 1, &H0001

		Dim oXMLDOM
		set oXMLDOM = Server.CreateObject("MSXML.DOMDocument")	
		set oXMLDOM.documentElement = oXMLDOM.createElement("result")
		
		Dim root
		set root = oXMLDOM.documentElement		
		
		set oNode = oXMLDOM.createElement("Original")
		set oOriginal = root.appendChild(oNode)
		
		set oNode = oXMLDOM.createElement("PageProperty")'
		set oPageProperty = root.appendChild(oNode)
		
		If rs.RecordCount = 0 Then
			
			set oNode = oXMLDOM.createElement("ShowPage")'
			oNode.text = sShowPage
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("PageSize")'
			oNode.text = sPageSize
			oPageProperty.appendChild oNode  		
				
			set oNode = oXMLDOM.createElement("NumOfPages")'
			oNode.text = 0
			oPageProperty.appendChild oNode  
				
			set oNode = oXMLDOM.createElement("OrderBy")'
			oNode.text = sOrderBy
			oPageProperty.appendChild oNode  	
				
			set oNode = oXMLDOM.createElement("OrderType")'
			oNode.text = sOrderType
			oPageProperty.appendChild oNode  			

			'GetPage = oXMLDOM.xml 
			set GetPage = oXMLDOM
				
			rs.Close
			Set rs = Nothing		
			Exit Function
		End If

		rs.PageSize = sPageSize
	    
		Dim NumOfPages
		NumOfPages = rs.PageCount
		If sShowPage < 1 then
			sShowPage = 1
		ElseIf sShowPage > NumOfPages Then
			sShowPage = NumOfPages
		End If
		rs.AbsolutePage = sShowPage

		Dim i
		i = 0
		Dim Field
		Dim oNode

							
		Do Until rs.EOF Or i = sPageSize

			set oNode = oXMLDOM.createElement("Row")
			oOriginal.appendChild oNode
					
			For each Field in rs.Fields 

				

				set oNode = oXMLDOM.createElement(Field.Name)
				if Not LenB(rs(Field.Name).Value)=0  Then
					if Field.type = 7 then 'date
						oNode.text = FormatDateTime(rs(Field.Name).Value,0)'kalau tdk pakai Cstr,format date bisa berubah
					else
						oNode.text = Cstr(rs(Field.Name).Value)'kalau tdk pakai Cstr,format date bisa berubah, toString
					end if					
					
				end if
				oOriginal.lastChild.appendChild oNode 
			Next
	        
			rs.MoveNext
			i = i + 1
		Loop
			
		set oNode = oXMLDOM.createElement("ShowPage")'
		oNode.text = sShowPage
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("PageSize")'
		oNode.text = sPageSize
		oPageProperty.appendChild oNode  		
			
		set oNode = oXMLDOM.createElement("NumOfPages")'
		oNode.text = NumOfPages
		oPageProperty.appendChild oNode  
			
		set oNode = oXMLDOM.createElement("OrderBy")'
		oNode.text = sOrderBy
		oPageProperty.appendChild oNode  	
			
		set oNode = oXMLDOM.createElement("OrderType")'
		oNode.text = sOrderType
		oPageProperty.appendChild oNode  			

		'GetPage = oXMLDOM.xml 
		set GetPage = oXMLDOM

		rs.Close
		Set rs = Nothing
	End Function
	
End Class



Class clsPost

	Private sConnectString
	Private sTableName
	Private arrFieldList
	Private arrDataTypes
	Private arrPrimaryKeys
	Private arrPKDataTypes
	Private sFormatDate
	Private sDateSeparator
	
	Public Property Let ConnectString(ByRef vVal)
		sConnectString = vVal
	End Property
	
	Public Property Let TableName(ByRef vVal)
		sTableName = vVal
	End Property	
	
	Public Property Let FieldList(ByRef vVal)
		arrFieldList = vVal
	End Property	
	
	Public Property Let DataTypes(ByRef vVal)
		arrDataTypes = vVal
	End Property
	
	Public Property Let PrimaryKeys(ByRef vVal)
		arrPrimaryKeys = vVal
	End Property	
	
	Public Property Let PKDataTypes(ByRef vVal)
		arrPKDataTypes = vVal
	End Property
	
	Public Property Let FormatDate(ByRef vVal)
		sFormatDate = vVal
	End Property	
	
	Public Property Let DateSeparator(ByRef vVal)
		sDateSeparator = vVal
	End Property					
	
	Public Function Post()
		'*****************************************************************************
		'	Count Number Of Fields Array & Number Of Primary Keys Array
		'*****************************************************************************

		Dim Item
		NumOfArrFields = 0
		For Each Item In arrFieldList
			NumOfArrFields = NumOfArrFields + 1
		Next
		NumOfArrKeys = 0
		For Each Item In arrPrimaryKeys
			NumOfArrKeys = NumOfArrKeys + 1
		Next

		'*****************************************************************************
		'	GET SUBMITTED DATA
		'*****************************************************************************

		set cn = server.CreateObject("ADODB.Connection")
		cn.Open sConnectString

		Dim oDoc
		set oDoc = Server.CreateObject("MSXML.DOMDocument")
		oDoc.async = false
		oDoc.load(Request)

		set oNodeOriginal = oDoc.documentElement.selectSingleNode("YASP_Original")
		set oNodeModified = oDoc.documentElement.selectSingleNode("YASP_Modified")
		set oNodeDeleted = oDoc.documentElement.selectSingleNode("YASP_Deleted")
		set oNodeNew = oDoc.documentElement.selectSingleNode("YASP_New")

		'*****************************************************************************
		'	Prepare for error report
		'*****************************************************************************

		Dim domErr
		set domErr = Server.CreateObject("MSXML.DOMDocument")	
		set domErr.documentElement = domErr.createElement("ErrorReport")

		Dim domErrRoot
		set domErrRoot = domErr.documentElement	

		'*****************************************************************************
		'	UPDATE RECORDS
		'*****************************************************************************

		If oNodeModified.childNodes.length > 0 Then
			For i=0 to oNodeModified.childNodes.length-1 'FOR EACH RECORDS

				'UPDATE - STEP 1 : Construct field & value pairs
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
				
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then 
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = '" & GetFieldValue(oNodeModified.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
									sFieldValues = sFieldValues & ", " & sField & " = null"
								else
									sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
								end if
							end if						
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then

								if GetFieldValue(oNodeModified.childNodes(i),sField) = "" then
										sFieldValues = sFieldValues & ", " & sField & " = null" 'un
								else
										sDate = GetFieldValue(oNodeModified.childNodes(i),sField)
								
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2
	
										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										if sTimePart <> "" then
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & " " & sTimePart & "#"
										else
											sFieldValues = sFieldValues & ", " & sField & " = #" & sDatePart & "#"
										end if
								end if
								
							end if					
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeModified.childNodes(i),sField)) then
								sFieldValues = sFieldValues & ", " & sField & " = " & GetFieldValue(oNodeModified.childNodes(i),sField)
							end if	
					End select
				Next
				sFieldValues = mid(sFieldValues,2)'Remove the first comma ","

				'UPDATE - STEP 2 : Construct primary key conditions
				sKeyFields = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sKey = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)

					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = '" & GetKeyFieldValue(oNodeModified.childNodes(i),sKey) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetKeyFieldValue(oNodeModified.childNodes(i),sKey)) then
								sKeyFields = sKeyFields & " AND " & sKey & " = " & GetKeyFieldValue(oNodeModified.childNodes(i),sKey)
							end if	
							
					End select			
				Next
				sKeyFields = mid(sKeyFields,5) 'Remove the first " AND"
				
				'UPDATE - STEP 3 : Construct SQL Statement
				SQL = "UPDATE " & sTableName & " SET " & sFieldValues & " WHERE " & sKeyFields

				'UPDATE - STEP 4 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'UPDATE - STEP 5 : Construct Error Report
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = oNodeModified.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "UPDATE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if	
				
				
			Next
		End If

		'*****************************************************************************
		'	DELETE RECORDS
		'*****************************************************************************
		If oNodeDeleted.childNodes.length > 0 Then

			For i=0 to oNodeDeleted.childNodes.length-1 'FOR EACH RECORDS
			
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValue = ""
				if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),"ID")) then
					sFieldValue = GetFieldValue(oNodeDeleted.childNodes(i),"ID")
				end if	
				
				'DELETE - STEP 1 : Construct primary key conditions
				sFieldValues = ""
				For j = 0 to CInt(NumOfArrKeys)-1
					sField = arrPrimaryKeys(j)
					sType = arrPKDataTypes(j)
					Select case sType 'Type for Primary Key : TEXT, NUMBER
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = '" & GetFieldValue(oNodeDeleted.childNodes(i),sField) & "'"
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeDeleted.childNodes(i),sField)) then
								sFieldValues = sFieldValues & " AND " & sField & " = " & GetFieldValue(oNodeDeleted.childNodes(i),sField)
							end if						
					End select
				Next
				sFieldValues = mid(sFieldValues,5) 'Remove the first " AND"

				'DELETE - STEP 2 : Construct SQL Statement
				SQL = "DELETE FROM " & sTableName & " WHERE " & sFieldValues
					
				'DELETE - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'DELETE - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = oNodeOriginal.childNodes(i).cloneNode(true)
						set domErrOriginal = domErrRow.appendChild(oNode)	
						
						set oNode = domErr.createElement("Modified")
						domErrRow.appendChild(oNode)
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "DELETE"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next
		End If

		'*****************************************************************************
		'	INSERT NEW RECORD
		'*****************************************************************************
		If oNodeNew.childNodes.length > 0 Then

			For i=0 to oNodeNew.childNodes.length-1

				'INSERT - STEP 1 : Construct fields & values
				sFields = ""
				sValues = ""
				For j = 0 to CInt(NumOfArrFields)-1
					sField = arrFieldList(j)
					sType = arrDataTypes(j)
					Select case sType
						case "TEXT" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
									sFields = sFields & "," & sField
									sValues = sValues & ",null"
								else
									sFields = sFields & "," & sField
									sValues = sValues & ",'" & GetFieldValue(oNodeNew.childNodes(i),sField) & "'"
								end if
							end if	
						case "NUMBER" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ", null"
								else
										sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
								end if						
							end if	
						case "DATE" :
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								if GetFieldValue(oNodeNew.childNodes(i),sField) = "" then
										sValues = sValues & ",null"
								else
							
										sDate = GetFieldValue(oNodeNew.childNodes(i),sField)
										sDatePart = split(sDate," ")(0)
										sTimePart = mid(sDate,len(sDatePart)+1) 'or 2

										d0 = split(sDatePart,sDateSeparator)(0)
										d1 = split(sDatePart,sDateSeparator)(1)
										d2 = split(sDatePart,sDateSeparator)(2)
										Select case sFormatDate
											case "ddMMyyyy","dMyyyy" :
												sDatePart = d1 & "/" & d0 & "/" & d2
											case "MMddyyyy","Mdyyyy" :
												sDatePart = d0 & "/" & d1 & "/" & d2
											case "yyyyMMdd","yyyyMd" :
												sDatePart = d1 & "/" & d2 & "/" & d0
										End Select
										
										if sTimePart <> "" then
											sValues = sValues & ",#" & sDatePart & " " & sTimePart & "#"
										else
											sValues = sValues & ",#" & sDatePart & "#"
										end if	
								end if							
							end if	
						case "BOOLEAN" : '(value must be -1/0 or true/false)
							if Not IsNull(GetFieldValue(oNodeNew.childNodes(i),sField)) then
								sFields = sFields & "," & sField
								sValues = sValues & "," & GetFieldValue(oNodeNew.childNodes(i),sField)
							end if					
					End select
				Next
				sFields = mid(sFields,2)'Remove the first comma ","
				sValues = mid(sValues,2)'Remove the first comma ","
				
				'INSERT - STEP 2 : Construct SQL Statement
				SQL = "Insert Into " & sTableName & " (" & sFields & ") VALUES (" & sValues & ")"

				'INSERT - STEP 3 : Execute SQL Statement
				on error resume next
				cn.Execute SQL
				
				'INSERT - STEP 4 : Construct Error Report	
				if err<>0 then
						set oNode = domErr.createElement("Row")
						set domErrRow = domErrRoot.appendChild(oNode)			

						set oNode = domErr.createElement("Original")
						domErrRow.appendChild(oNode)
						
						'set oNode = domErr.createElement("Modified")
						'domErrRow.appendChild(oNode)
						set oNode = oNodeNew.childNodes(i).cloneNode(true)
						domErrRow.appendChild(oNode)'Utk Insert error, apa yg di-insert dimasukkan ke  node "Modified"	
						
						set oNode = domErr.createElement("SQL")
						set domErrSQL = domErrRow.appendChild(oNode)
						domErrSQL.text = SQL	
						
						set oNode = domErr.createElement("Action")
						set domErrAction = domErrRow.appendChild(oNode)
						domErrAction.text = "INSERT"
						
						set oNode = domErr.createElement("Source")
						set domErrSource = domErrRow.appendChild(oNode)
						domErrSource.text = err.Source 
						
						set oNode = domErr.createElement("Number")
						set domErrNumber = domErrRow.appendChild(oNode)
						domErrNumber.text = err.number
						
						set oNode = domErr.createElement("Description")
						set domErrDescription = domErrRow.appendChild(oNode)
						domErrDescription.text = err.Description
						
						err.Clear 
				end if
			Next

		End If

		cn.Close 
		set cn = nothing
		
		set Post = domErr
		
	End Function


	'*****************************************************************************
	'	Generic Functions
	'*****************************************************************************
	Private Function GetFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			if oNode.getElementsByTagName(FieldName)(0).attributes.length = 0 then 'get yg bukan identity (identity berarti ada attribute IsIdentity=true)
				GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
			else
				GetFieldValue = null
			end if
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetFieldValue = null
		end if
	End Function
	
	Private Function GetKeyFieldValue(oNode,FieldName)
		if oNode.getElementsByTagName(FieldName).length = 1 then
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(0).text)
		elseif oNode.getElementsByTagName(FieldName).length = 2 then '=2 berarti yg diedit adl identity
			GetKeyFieldValue = FixSQL(oNode.getElementsByTagName(FieldName)(1).text)'yg pertama(0) = updated, yg kedua(1) = identity
		else
			GetKeyFieldValue = null
		end if
	End Function	

	Private Function FixSQL(str)
		FixSQL =  Replace(str,"'","''")
	End Function

End Class
%> 
<%
Dim oPost
Set oPost = New clsPost
oPost.ConnectString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Server.MapPath("\database/grid.mdb")
oPost.TableName = "Test"
oPost.FieldList = Array("CaseDesc","TestResult","Comment","NeedToFix")
oPost.DataTypes = Array("TEXT","TEXT","TEXT","BOOLEAN")
oPost.PrimaryKeys = Array("TestID")
oPost.PKDataTypes = Array("NUMBER")

Dim oDocErr
set oDocErr = oPost.Post()

Dim oQuery
Set oQuery = New clsQuery
oQuery.ConnectString = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" & Server.MapPath("\database/grid.mdb")
oQuery.SQL = "SELECT * FROM Test"
oQuery.PageSize	= Request.QueryString("PageSize")
oQuery.ShowPage	= Request.QueryString("ShowPage")
oQuery.OrderBy	= Request.QueryString("OrderBy")
oQuery.OrderType = Request.QueryString("OrderType")

Response.ContentType = "text/xml"
Response.Write "<?xml version='1.0' encoding='ISO-8859-1'?>" & vbCRLF 
Dim oDocResult
set oDocResult = oQuery.GetPage()

'Add Error Report
set oNode = oDocErr.documentElement
oDocResult.documentElement.appendChild(oNode) 
Response.Write oDocResult.xml
%>

Next, we can use this samplePost1.asp together with sample1.htm (contains WebGrid EXP). To specify the server page that we use to post XML data is as follows (see the sample1.htm illustration before):

obj1.UrlPOST     =	"dataPOST/samplePost1.asp" 

Up until now, we have finished creating two pages for reading and updating data from and to database which are sampleGet1.asp & samplePost1.asp. Next, we are going to see the sample of using these together in a simple application.

Our First Project

We are going to use the examples above to create our first project. First, make sure all files needed are prepared already. WebGrid EXP files are : WebGridEXP.js, WebGridEXP.css, empty.jpg & lookup.gif. Besides that, we also include DataAccess.asp as mentioned in above explanation. In this example, we are going to put all of these files into : include folder. We use Access database, which we put in the database folder.

For the ASP page that gets data from database (sampleGet1.asp), we put it in dataGET folder.
For the ASP page that post data to database (samplePost1.asp), we put it in dataPOST folder.
For the HTML page that contains the WebGRID EXP (sample1.htm), we put it in root folder.

Below is the project structure (folder structure can be re-arranged to suit your needs. Note : this is only for sample purpose):

Here you can see the result.