I've run into a number of problems with listing 8-35 (in vb
.net). Following is a list:
Dim AsyncResult as SqlAsyncResult
I don't believe there is an SqlAsyncResult class in the .Net framework. I believe, based upon the prior code and useage that this should be IAsyncResult
AsyncResult = Command.BeginExecuteReader(New AsyncCallback(AddressOf CBMethod), CommandBehavior.CloseConnection)
I had some trouble with this code as well. I finally figured out that my problem was the result of naming my page "AsyncCallback" which created a class of the same name, which conflicted with the System.AsyncCallback class.
In the CBMethod() subroutine, there's also a problem:
OrderReader = ar.EndExecuteReader(ar)
In this case, they're expecting ar to act as both a SQLCommand object (with the EndExecuteReader() method) and an object that implements the IAsyncResult interface (as the argument to the EndExecuteReader).
I haven't quite figured this one out yet. If they want to use the SQLCommand object from the Page_Load, then it's declaration will have to be moved outside of the Page_Load procedure; otherwise, it won't be available to the CBMethod. I've done this, and it compiles, but my grid doesn't populate.
My CBMethod() sub looks like this (vb
Public Sub CBMethod(ByVal ar As IAsyncResult)
Dim OrdersReader As SqlDataReader
OrdersReader = command.EndExecuteReader(ar)
'If OrdersReader.HasRows Then
' Do While OrdersReader.Read()
' Dim x As Label = New Label
' x.Text = OrdersReader("CompanyName").ToString()
gvOrders.DataSource = OrdersReader
I've included some commented out code that I used to confirm, when stepping through in the debugger, that the CBMethod was, in fact being called, and that the OrdersReader was populated with data. In spite of confirming that the data reader does have data, the grid still wasn't populating with the OrdersReader was bound to it...
Just to be complete, here's the rest of my codebehind (just for the class):
Dim command As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dbConnection As SqlConnection = New SqlConnection()
Dim AsyncResult As IAsyncResult
dbConnection.ConnectionString = ConfigurationManager.ConnectionStrings("DSN_Northwind").ConnectionString
command = New SqlCommand()
'Selecting top 5 records from the Orders table
command.CommandText = "SELECT TOP 5 Customers.CompanyName, Customers.ContactName, Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate FROM Orders, Customers WHERE Orders.CustomerID = Customers.CustomerID ORDER BY Customers.CompanyName, Customers.ContactName"
command.CommandType = CommandType.Text
command.Connection = dbConnection
'Starting the asynchronous processing
AsyncResult = command.BeginExecuteReader(New AsyncCallback(AddressOf CBMethod), CommandBehavior.CloseConnection)
If anyone has gotten this to work, I'd love to see the corrected code.
Thanks in advance,