If FormState = Form_State.Add_RecordState Then
KeyPreview = True
Call ContextMnu()
Call CapsLock()
Me.BSave.Text = "Save:"
Me.Text = "Add Record:"
Call ImageSyntax()
Try
With OleCn
If .State <> ConnectionState.Open Then
.ConnectionString = StrConnection()
.Open()
'Initialized Command...
With OleDa
.SelectCommand = New OleDbCommand()
.SelectCommand.CommandText = "SELECT * FROM [Students]"
.SelectCommand.Connection = OleCn
End With
'Insert Command...
With OleDa
.InsertCommand = New OleDbCommand()
.InsertCommand.CommandText = "INSERT INTO [Students] ([Student_ID] , [FirstName] , [Date_Of_Birth] , [Date_Of_ou] , " & _
" [StudentPic]) VALUES (@Student_ID , @FirstName , @Date_Of_Birth , @Date_Of_ou , @StudentPic)"
.InsertCommand.Connection = OleCn
.InsertCommand.Parameters.Add("@Student_ID", OleDbType.VarWChar, 50, "Student_ID")
.InsertCommand.Parameters.Add("@FirstName", OleDbType.VarWChar, 50, "FirstName")
.InsertCommand.Parameters.Add("@Date_Of_Birth", OleDbType.VarWChar, 50, "Date_Of_Birth")
.InsertCommand.Parameters.Add("@Date_Of_ou", OleDbType.VarWChar, 50, "Date_Of_ou")
.InsertCommand.Parameters.Add("@StudentPic", OleDbType.Binary, 0, "StudentPic")
End With
'If Add or New record...Update
'Systax on how to update without using Commandbuilder...
With OleDa
.UpdateCommand = New OleDbCommand()
.UpdateCommand.CommandText = "UPDATE [Students] SET [FirstName] = @FirstName , " & _
"[Date_Of_Birth] = @Date_Of_Birth , [Date_Of_ou] = @Date_Of_ou , [StudentPic] = @StudentPic WHERE ([Student_ID] = @Student_ID_Syntax)"
.UpdateCommand.Connection = OleCn
.UpdateCommand.Parameters.Add("@FirstName", OleDbType.VarWChar, 50, "FirstName")
.UpdateCommand.Parameters.Add("@Date_Of_Birth", OleDbType.VarWChar, 50, "Date_Of_Birth")
.UpdateCommand.Parameters.Add("@Date_Of_ou", OleDbType.VarWChar, 50, "Date_Of_ou")
.UpdateCommand.Parameters.Add("@StudentPic", OleDbType.Binary, 0, "StudentPic")
'To handle error like (Concurrency violation: The UpdateCommand affected 0 of the expected 1 records...)
'DataRow Version Default(Execute scalar)
.UpdateCommand.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Student_ID_Syntax", System.Data.OleDb.OleDbType.VarWChar, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), "Student_ID", _
DataRowVersion.Default, True))
End With
End If
End With
Catch ex As Exception
MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error...")
Finally
OleCn.Close()
End Try
Exit Sub
End If
If FormState = Form_State.Edit_RecordState Then
KeyPreview = True
Call ContextMnu()
Call CapsLock()
Me.BSave.Text = "Update:"
Me.Text = "Edit Record:"
Try
With OleCn
If .State <> ConnectionState.Open Then
.ConnectionString = StrConnection()
.Open()
With OleDa
.SelectCommand = New OleDbCommand()
.SelectCommand.CommandText = "SELECT * FROM [Students] WHERE (Student_ID = @Student_ID)"
.SelectCommand.Parameters.Add("@Student_ID", OleDbType.VarChar, 50, "Student_ID").Value = ShowBind.ToString()
.SelectCommand.Connection = OleCn
End With
'If Modify or Edit record...Update
'Systax on how to update without using Commandbuilder...
With OleDa
.UpdateCommand = New OleDbCommand()
.UpdateCommand.CommandText = "UPDATE [Students] SET [Student_ID] = @Student_ID , [FirstName] = @FirstName " & _
"[Date_Of_Birth] = @Date_Of_Birth , [Date_Of_ou] = @Date_Of_ou , [StudentPic] = @StudentPic WHERE ([Student_ID] = @Student_ID_Syntax)"
.UpdateCommand.Connection = OleCn
.UpdateCommand.Parameters.Add("@Student_ID", OleDbType.VarWChar, 50, "Student_ID")
.UpdateCommand.Parameters.Add("@FirstName", OleDbType.VarWChar, 50, "FirstName")
.UpdateCommand.Parameters.Add("@Date_Of_Birth", OleDbType.VarWChar, 50, "Date_Of_Birth")
.UpdateCommand.Parameters.Add("@Date_Of_ou", OleDbType.VarWChar, 50, "Date_Of_ou")
.UpdateCommand.Parameters.Add("@StudentPic", OleDbType.Binary, 0, "StudentPic")
'To handle error like (Concurrency violation: The UpdateCommand affected 0 of the expected 1 records...)
'DataRow Version Original...
.UpdateCommand.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Student_ID_Syntax", System.Data.OleDb.OleDbType.VarWChar, 50, _
System.Data.ParameterDirection.Input, False, _
CType(0, Byte), CType(0, Byte), "Student_ID", _
DataRowVersion.Original, True))
End With
DtaSet2.Clear()
OleDa.Fill(DtaSet2, "Students")
Me.TxtStudentID.DataBindings.Add(New Binding("Text", DtaSet2, "Students.Student_ID", True))
Me.TxtFirstName.DataBindings.Add(New Binding("Text", DtaSet2, "Students.FirstName", True))
Me.DTDateOfBirth.DataBindings.Add(New Binding("Text", DtaSet2, "Students.Date_Of_Birth", True))
Me.textou.DataBindings.Add(New Binding("Text", DtaSet2, "Students.Date_Of_ou", True))
Main = FrmMain
Me.StudentPic.Image = FrmMain.PictureBox1.Image
End If
End With
Catch ex As Exception
MsgBox(ex.Message(), MsgBoxStyle.Critical, "Error...")
Finally
OleCn.Close()
End Try
Exit Sub
End If
End Sub
Private Function RequiredEntry() As Boolean
If TxtStudentID.Text = "" Or TxtFirstName.Text = "" = True Then
MsgBox("Please enter required * information....", MsgBoxStyle.Critical, "Attention...")
Return True
Exit Function
End If
End Function
Private Sub BSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSave.Click
Main = FrmMain
If FormState = Form_State.Add_RecordState Then
If RequiredEntry() = True Then
Return
End If
Try
Dim DtaRow As DataRow
Dim MemStream As New MemoryStream
Dim DataPic_Insert As Byte()
DtaRow = DtaSet.Tables("Students").NewRow
With DtaRow
.Item("Student_ID") = IIf(TxtStudentID.Text = "", System.DBNull.Value, TxtStudentID.Text)
.Item("FirstName") = IIf(TxtFirstName.Text = "", System.DBNull.Value, TxtFirstName.Text)
.Item("Date_Of_Birth") = IIf(DTDateOfBirth.Text = "", System.DBNull.Value, DTDateOfBirth.Text)
.Item("Date_Of_ou") = IIf(textou.Text = "", System.DBNull.Value, textou.Text)
Me.StudentPic.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
DataPic_Insert = MemStream.GetBuffer()
MemStream.Read(DataPic_Insert, 0, MemStream.Length)
.Item("StudentPic") = DataPic_Insert
End With
DtaSet.Tables("Students").Rows.Add(DtaRow)
OleDa.Update(DtaSet, "Students")
DtaSet.AcceptChanges()
'Focus the Added record...
With Main
.BindingContext.Item(DtaSet, "Students").Position = Main.BindingContext.Item(DtaSet, "Students").Count - 1
Call .Navigation()
End With
MsgBox("Record's successfully saved...", MsgBoxStyle.Information, "Record's saved...")
If MsgBox("Do you want to add new record?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirmation...") = MsgBoxResult.Yes Then
Me.FormState = Form_State.Add_RecordState
Call ResetEntry()
TxtStudentID.Focus()
Exit Sub
End If
Me.Close()
Catch ex As Exception
MsgBox(ex.Message(), MsgBoxStyle.Exclamation, "Sorry, unable to save...")
'if Duplicate Student ID...
TxtStudentID.Focus()
TxtStudentID.SelectAll()
DtaSet.RejectChanges()
End Try
Exit Sub
End If
If FormState = Form_State.Edit_RecordState Then
If RequiredEntry() = True Then
Return
End If
Try
Dim MemStream As New MemoryStream
Dim DataPic_Update As Byte()
With DtaSet2
.Tables("Students").Rows(0).Item("Student_ID") = IIf(TxtStudentID.Text = "", System.DBNull.Value, TxtStudentID.Text)
.Tables("Students").Rows(0).Item("FirstName") = IIf(TxtFirstName.Text = "", System.DBNull.Value, TxtFirstName.Text)
.Tables("Students").Rows(0).Item("Date_Of_Birth") = IIf(DTDateOfBirth.Text = "", System.DBNull.Value, DTDateOfBirth.Text)
.Tables("Students").Rows(0).Item("Date_Of_ou") = IIf(textou.Text = "", System.DBNull.Value, textou.Text)
Me.StudentPic.Image.Save(MemStream, Imaging.ImageFormat.Jpeg)
DataPic_Update = MemStream.GetBuffer()
MemStream.Read(DataPic_Update, 0, MemStream.Length)
.Tables("Students").Rows(0).Item("StudentPic") = DataPic_Update
End With
BindingContext(DtaSet2, "Students").EndCurrentEdit()
If DtaSet2.HasChanges() Then
OleDa.Update(DtaSet2, "Students")
DtaSet2.AcceptChanges()
End If
'Focus updated record without load or refresh...
With Main
With .DgV
.SelectedRows(0).Cells(0).Value = Me.TxtStudentID.Text
.SelectedRows(0).Cells(1).Value = Me.TxtFirstName.Text
.SelectedRows(0).Cells(2).Value = Me.DTDateOfBirth.Text
.SelectedRows(0).Cells(3).Value = Me.textou.Text
End With
.PictureBox1.Image = Me.StudentPic.Image
End With
MsgBox("Record's successfully updated...", MsgBoxStyle.Information, "Record's Updated...")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Sorry, Unable to update...")
'If Duplicate Student ID Back to updated Student ID (Undo)
Me.TxtStudentID.Text = Main.DgV.SelectedRows(0).Cells(0).Value
End Try
Exit Sub
End If