Datasheet

mDiaryEntry is a global variable used to hold the DiaryEntry object relating to the day being edited.
The constructor, shown in the following code, does all the hard work of actually getting the data:
Public Sub New(ByVal DiaryId As Integer, ByVal EntryDate As Date)
mDiaryId = DiaryId
If mDiaryId > 0 Then
Try
Dim diaryDBConn As New SqlConnection(conString)
Dim sqlString As String = “GetDiaryEntryByDate”
Dim sqlCmd As New SqlCommand(sqlString, diaryDBConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue(“@DiaryId”, mDiaryId)
sqlCmd.Parameters.AddWithValue(“@EntryFromDate”, EntryDate)
sqlCmd.Parameters.AddWithValue(“@EntryToDate”, EntryDate)
diaryDBConn.Open()
Dim diaryEntrySQLDR As SqlDataReader =
sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
sqlCmd = Nothing
If diaryEntrySQLDR.Read() Then
mDiaryEntryId = CLng(diaryEntrySQLDR(“DiaryEntryId”))
mEntryDate = CDate(diaryEntrySQLDR(“EntryDate”))
mEntryTitle = diaryEntrySQLDR(“EntryTitle”).ToString
mEntryText = diaryEntrySQLDR(“EntryText”).ToString
Else
mDiaryEntryId = -1
mEntryDate = EntryDate
End If
diaryEntrySQLDR.Close()
diaryEntrySQLDR = Nothing
diaryDBConn.Close()
diaryDBConn = Nothing
Catch ex As Exception
mDiaryEntryId = -1
End Try
End If
End Sub
The GetDiaryEntryByDate stored procedure is called to get the data. If there isn’t an existing entry for
that day,
mDiaryEntryId is set to -1 and all the other properties are left at their default values.
Otherwise they are populated with the data from the database.
27
The Online Diary and Organizer
04_749516 ch01.qxp 2/10/06 9:11 PM Page 27