(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: June 2002
Section: Improve ASP.NET Application Performance
Author: Paul Delcogliano

JavaScript	The Behavior Executes the showResultsUsingCallback Function
Listing 1	The behavior passes a result object as the methodResult parameter. You use this object to check for errors and populate the controls on the form.

function showResultsUsingCallback(methodResult) { 

// if there is an error, and the call came from 
// the call() in init() 
if (methodResult.error) { 

  // Pull the error information from the 
  // result.errorDetail object 
  reportError(methodResult.errorDetail); 
} 
else { 
  // The value property contains the object 
  // I used to pass results back from the 
  //Web service 
  var oCustomResultsObject = methodResult.value; 

  // Show the results in the HTML form 
  document.frmProduct.
     <%=txtCompanyName.UniqueId%>.value = 
  	  oCustomResultsObject.Company; 
  document.frmProduct.
     <%=txtUnitPrice.UniqueId%>.value = 
	  oCustomResultsObject.UnitPrice; 
  document.frmProduct.
     <%=txtCategory.UniqueId%>.value = 
	  oCustomResultsObject.Category; 
  document.frmProduct.
     <%=txtQuantityPerUnit.UniqueId%>.value = 
	  oCustomResultsObject.QuantityPerUnit; 
  document.frmProduct.
     <%=txtUnitsInStock.UniqueId%>.value = 
	  oCustomResultsObject.UnitsInStock; 
  document.frmProduct.
     <%=txtUnitsOnOrder.UniqueId%>.value = 
	  oCustomResultsObject.UnitsOnOrder; 

} 
}

VB.NET	This Web Method Executes the GetProductDetails Stored Procedure
Listing 2	This Web method executes the  GetProductDetails stored procedure. The method uses a SQLDataReader object to get the results from the stored procedure. Next, data read from the SqlDataReader is added to the oResults object through the object's  constructor. Finally, the method returns the oResults object to the caller. 

<WebMethod(EnableSession:=True)> Public _
   Function GetProductDetails(ByVal iProductId _
   As Int32) As cResults

	Dim comLocal As SqlCommand
	Dim drLocal As SqlDataReader
	Dim oResults As cResults

	Try

		comLocal = New SqlCommand( _
			"GetProductDetails", New _
			SqlConnection(Session("DBConnection"))
		comLocal.CommandType = _
			CommandType.StoredProcedure
		comLocal.Parameters.Add(New _
			SqlParameter("@iProductId", _
			iProductId))
		comLocal.Connection.Open()
		drLocal = comLocal.ExecuteReader( _
			CommandBehavior.CloseConnection)

		With drLocal

			If .Read() Then

				' If a row is returned from the 
				' query then assign values from 
				' SQLDataReader to properties of 
				' Results class
				oResults = New _
					cResults(.Item("CompanyName"), _
					CType(.Item("UnitPrice"), _
					Single).ToString("C2"), _
					.Item("CategoryName"), _
					.Item("QuantityPerUnit"), _
					.Item("UnitsInStock").ToString, _
					.Item("UnitsOnOrder").ToString)

				End If

			End With

		Catch e As System.Exception
			' In the "real world" the error should 
			' be logged to a file, db, or event log
			Throw e
		Finally

			' Close the datareader 
			If Not drLocal.IsClosed Then
				drLocal.Close()
			End If

			' Clean-up the database connection
			comLocal.Dispose()

			' Return the values from the database 
			' using the local 
			' instance of the cResults class
			GetProductDetails = oResults

		End Try

	End Function
