Code:
Private SQLInsert As New OleDbCommand("INSERT INTO Recipe (Routine, Description, Snippet,ParentID) VALUES (@Name, @Descr, @Snip,@ID)", Me.connection)
Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Try
If Len(txRoutine.Text) < 1 Then
MsgBox("Please enter recipe name.", MsgBoxStyle.OkOnly, "Validation error")
txRoutine.Focus()
Exit Sub
End If
If Len(RichTextBox1.Text) < 1 Then
MsgBox("Please enter Ingredients.", MsgBoxStyle.OkOnly, "Validation error")
RichTextBox1.Focus()
Exit Sub
End If
If Len(RichTextBox2.Text) < 1 Then
MsgBox("Please enter Process.", MsgBoxStyle.OkOnly, "Validation error")
RichTextBox2.Focus()
Exit Sub
End If
' checks to see if name is already taken
If Len(txRoutine.Text) > 100 Then
MsgBox("Title of recipe is too long", MsgBoxStyle.OkOnly, "Validation error")
txRoutine.Focus()
Exit Sub
End If
' check to see if we are adding a new one or updating a record
If Not tvEdit Then
' MsgBox("we are adding")
SQLInsert.Parameters.AddWithValue("@Name", CStr(Replace(txRoutine.Text, "'", "`")))
SQLInsert.Parameters.AddWithValue("@Descr", CStr(Replace(RichTextBox1.Text, "'", "`")))
SQLInsert.Parameters.AddWithValue("@Snip", CStr(Replace(RichTextBox2.Text, "'", "`")))
SQLInsert.Parameters.AddWithValue("@ID", Label3.Text)
'connection.Open()
SQLInsert.ExecuteNonQuery()
Me.adapter.InsertCommand = SQLInsert
Dim row = table.NewRow()
row("Routine") = CStr(Replace(txRoutine.Text, "'", "`"))
row("Description") = CStr(Replace(RichTextBox1.Text, "'", "`"))
row("Snippet") = CStr(Replace(RichTextBox2.Text, "'", "`"))
row("ParentID") = Label3.Text
table.Rows.Add(row)
' add to listview
Else
' MsgBox("we are edting")
SQLupdate.Parameters.AddWithValue("@Name", CStr(Replace(txRoutine.Text, "'", "`")))
SQLupdate.Parameters.AddWithValue("@Descr", CStr(Replace(RichTextBox1.Text, "'", "`")))
SQLupdate.Parameters.AddWithValue("@Snip", CStr(Replace(RichTextBox2.Text, "'", "`")))
SQLupdate.Parameters.AddWithValue("@ID", LblCounter.Text)
'connection.Open()
SQLupdate.ExecuteNonQuery()
Me.adapter.UpdateCommand = SQLupdate
Dim row = table.NewRow()
row("Routine") = CStr(Replace(txRoutine.Text, "'", "`"))
row("Description") = CStr(Replace(RichTextBox1.Text, "'", "`"))
row("Snippet") = CStr(Replace(RichTextBox2.Text, "'", "`"))
row("ParentID") = LblCounter.Text
table.Rows.Add(row)
' Me.adapter.Update(Me.table)
End If
' End Using
MsgBox("Recipe: " & txRoutine.Text & vbCrLf & " Has been saved successfully")
BtnDegree.Visible = False
tvwMain.Show()
txRoutine.Visible = False
BtnAbort.Visible = False
BtnSave.Visible = False
LblRecipeName.Visible = False
RichTextBox1.ReadOnly = True
RichTextBox2.ReadOnly = True
' reload treeview
RefreshTree()
Catch ex As Exception
MsgBox("Failed to Save Recipe" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Save")
Debug.WriteLine(ex.Message)
End Try
End Sub