Datasheet
The first line sets the CurrentPageIndex of the grid to 0, having the effect of starting the grid at page 1.
We do this because we are re-sorting. We then set 
SortField to the sorted field, and rebind the grid.
Notice that 
SortField hasn't been declared as a variable – in fact it's a property. This might seem 
confusing because properties are always attached to objects, prompting the question what object is this
one attached to. Well since it hasn't got a named object, ASP.NET takes this as being a property of the
current 
Page. By default, a Page doesn't have a SortField property, so we define one:
Property SortField() As String
Get
Dim o As Object = ViewState("SortField")
If o Is Nothing Then
Return String.Empty
End If
Return CStr(o)
End Get
Set(ByVal Value As String)
ViewState("SortField") = Value
End Set
End Property
The interesting point is that we haven't defined a class. Because we are coding within an ASP.NET page,
the 
Page is a class, so all we are doing is adding a property to the page (for the purpose of referencing
the sorted field later when we bind the grid). When the page is run, ASP.NET adds your code to the class
for the page. It's not like the examples in the previous chapter, where we were creating a separate 
class – here we want our property to be part of the same class as the rest of the code.
The 
Get part of the property first fetches the sort value from the ViewState into an object variable (all
items in 
ViewState are returned as objects), and then checks to see if the object is Nothing. This would
be the case if the sort hasn't been defined, such as the first time the page is loaded. If it is 
Nothing, then
an empty string is returned, otherwise the object is converted to a string with 
CStr and that is returned.
This is a perfectly safe conversion because we know that the 
ViewState for this item only contains a
string, as that's what the 
Set part of the property does. ViewState was covered in Chapter 6.
Using 
String.Empty is a special way of defining an empty string, and avoids having to use open and
close quotation marks next to each other, where it's often difficult to see if there is a space between the
quotation marks.
Now let's look at the 
BindGrid() routine:
Sub BindGrid()
The first two lines define string variables to hold the connection string and the text for the command to
run. Notice that the connection string has been changed to an Access one:
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\BegASPNet11\data\Northwind.mdb"
Dim CommandText As String
263
Reading From Databases
57076_Ch 8 SAN.qxd 01/12/2003 6:43 PM Page 263










