Datasheet
When the diary title or entry boxes are changed, mDiaryEntry is updated:
Protected Sub entryTitleTextBox_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles entryTitleTextBox.TextChanged
mDiaryEntry.EntryTitle = entryTitleTextBox.Text
End Sub
Protected Sub entryTextTextBox_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles entryTextTextBox.TextChanged
mDiaryEntry.EntryText = entryTextTextBox.Text
End Sub
Saving changes occurs when you click the Save button:
Protected Sub saveDiaryEntryButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles saveDiaryEntryButton.Click
mDiaryEntry.Save()
End Sub
All that’s involved is calling the Save() method of the DiaryEntry object:
Public Sub Save()
If mDiaryEntryId = -1 Then
InsertNewDiaryEntry()
Else
UpdateDiaryEntry()
End If
End Sub
Based on whether or not mDiaryEntryId is -1, the method either inserts a new entry into the database
or updates an existing one. The private method
InsertNewDiaryEntry() inserts a new diary entry:
Private Sub InsertNewDiaryEntry()
If mDiaryId <> -1 Then
Dim diaryDBConn As New SqlConnection(conString)
Dim sqlString As String = “InsertDiaryEntry”
Dim sqlCmd As New SqlCommand(sqlString, diaryDBConn)
sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.Parameters.AddWithValue(“@DiaryId”, mDiaryId)
sqlCmd.Parameters.AddWithValue(“@EntryDate”, mEntryDate)
sqlCmd.Parameters.AddWithValue(“@EntryTitle”, mEntryTitle)
sqlCmd.Parameters.AddWithValue(“@EntryText”, mEntryText)
sqlCmd.Parameters.Add(“@NewDiaryEntryId”, SqlDbType.BigInt)
sqlCmd.Parameters(“@NewDiaryEntryId”).Direction =
ParameterDirection.ReturnValue
diaryDBConn.Open()
sqlCmd.ExecuteNonQuery()
mDiaryEntryId = CLng(sqlCmd.Parameters(“@NewDiaryEntryId”).Value())
diaryDBConn.Close()
sqlCmd = Nothing
28
Chapter 1
04_749516 ch01.qxp 2/10/06 9:11 PM Page 28