Datasheet

However, the new ObjectDataSource lets you have the best of both: easy-to-use data controls and use of
classes to separate business, data, and presentation layers. Instead of connecting directly to a database,
the
ObjectDataSource takes its data from one of the classes. diaryEntriesObjectDataSource on
DiaryMain.aspx, for example, takes its data from the
GetDiaryEntriesRecentlyChanged() method
of the
DiaryEntry class, whose markup is shown here:
<asp:ObjectDataSource ID=”diaryEntriesObjectDataSource” runat=”server”
SelectMethod=”GetDiaryEntriesRecentlyChanged”
TypeName=”DiaryEntry”>
<SelectParameters>
<asp:SessionParameter DefaultValue=”-1” Name=”DiaryId”
SessionField=”DiaryId” Type=”Int32” />
</SelectParameters>
</asp:ObjectDataSource>
The TypeName attribute specifies the class name to use, and the SelectMethod attribute specifies which
method of that class will provide the data.
GetDiaryEntriesRecentlyChanged() is a shared method,
shown here:
Public Shared Function GetDiaryEntriesRecentlyChanged(ByVal DiaryId As Integer)
As SqlDataReader
Dim diaryDBConn As New SqlConnection(conString)
Dim sqlString As String = “GetRecentDiaryEntries”
Dim sqlCmd As New SqlCommand(sqlString, diaryDBConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue(“@DiaryId”, DiaryId)
diaryDBConn.Open()
Dim entrySQLDR As SqlDataReader =
sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
sqlCmd = Nothing
Return entrySQLDR
End Function
The method returns a SqlDataReader object populated with the data the ObjectDataSource control
will use.
Actually displaying the data is then just a matter of pointing a data-aware control at the
ObjectDataSource:
<asp:GridView ID=”recentEntriesGridView” runat=”server”
AutoGenerateColumns=”False”
Caption=”Recent Entries” CaptionAlign=”Left” CellPadding=”4”
DataSourceID=”diaryEntriesObjectDataSource”
ForeColor=”#333333” GridLines=”None” Style=”z-index: 105; left:
535px; position: absolute;
top: 321px” Width=”476px” Height=”208px”>
<FooterStyle BackColor=”#5D7B9D” Font-Bold=”True” ForeColor=”White”
/>
<RowStyle BackColor=”#F7F6F3” ForeColor=”#333333” />
<Columns>
<asp:BoundField DataField=”EntryDate” />
<asp:BoundField DataField=”EntryTitle” />
25
The Online Diary and Organizer
04_749516 ch01.qxp 2/10/06 9:11 PM Page 25