Code for CRUD form
Imports [Link]
Public Class CRUD
Dim con As OleDbConnection
Dim adapter As OleDbDataAdapter
Dim dataset As DataSet
Dim bindingSource As BindingSource
Dim cmd As OleDbCommand
Dim currentPosition As Integer = 0
Private Sub CRUD_Load(sender As Object, e As EventArgs) Handles [Link]
con = New OleDbConnection("Provider=[Link].4.0;Data Source=|
DataDirectory|\[Link]")
' Initialize DataGridView
[Link] = True
' Load data from database
LoadData()
End Sub
Private Sub LoadData()
' Initialize dataset and adapter
dataset = New DataSet
adapter = New OleDbDataAdapter("SELECT * FROM FORM3", con)
' Fill dataset
[Link](dataset, "FORM3")
' Initialize binding source
bindingSource = New BindingSource
[Link] = [Link]("FORM3")
' Bind data to DataGridView
[Link] = bindingSource
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles [Link]
' Check if the StudentID already exists
If DoesStudentIDExist([Link]) Then
[Link]("This Student ID already exists. Please enter a unique Student ID.")
Return
End If
' Insert data into database
cmd = New OleDbCommand("INSERT INTO FORM3 (StudentID, FName, Surname,
Class, Age, EnrollDate) VALUES (@StudentID, @FName, @Surname, @Class, @Age,
@EnrollDate)", con)
[Link]("@StudentID", [Link])
[Link]("@FName", [Link])
[Link]("@Surname", [Link])
[Link]("@Class", [Link])
[Link]("@Age", [Link])
[Link]("@EnrollDate", [Link])
' Open the connection, execute the command, and close the connection
[Link]()
Dim rowsAffected As Integer = [Link]()
[Link]()
' Check if any rows were updated
If rowsAffected > 0 Then
[Link]("Record saved successfully.", "Success", [Link],
[Link])
Else
[Link]("Record not saved.", "Error", [Link],
[Link])
End If
' Reload data
LoadData()
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles [Link]
Try
' Create the SQL command to update data in the database
cmd = New OleDbCommand("UPDATE FORM3 SET FName = @FName, Surname =
@Surname, Class = @Class, Age = @Age, EnrollDate = @EnrollDate WHERE StudentID =
@StudentID", con)
' Set parameter values
[Link]("@FName", [Link])
[Link]("@Surname", [Link])
[Link]("@Class", [Link])
[Link]("@Age", [Link])
[Link]("@EnrollDate", [Link])
[Link]("@StudentID", [Link])
' Open the connection, execute the command, and close the connection
[Link]()
Dim rowsAffected As Integer = [Link]()
[Link]()
' Check if any rows were updated
If rowsAffected > 0 Then
[Link]("Record updated successfully.", "Success",
[Link], [Link])
Else
[Link]("No record found with that Student ID.", "Error",
[Link], [Link])
End If
' Reload data
LoadData()
Catch ex As Exception
[Link]("An error occurred: " & [Link])
Finally
' Ensure the connection is closed in case of an error
If [Link] = [Link] Then
[Link]()
End If
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles [Link]
' Delete data from database
cmd = New OleDbCommand("DELETE FROM FORM3 WHERE StudentID =
@StudentID", con)
' Set the parameter value
[Link]("@StudentID", [Link])
' Open the connection, execute the command, and close the connection
[Link]()
Dim rowsAffected As Integer = [Link]()
[Link]()
' Check if any rows were updated
If rowsAffected > 0 Then
[Link]("Record deleted successfully.", "Success", [Link],
[Link])
Else
[Link]("Record not deleted.", "Error", [Link],
[Link])
End If
' Reload data
LoadData()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles [Link]
'Navigate to next record
If currentPosition < [Link]("FORM3").[Link] - 1 Then
currentPosition += 1
[Link] = currentPosition
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles [Link]
' Navigate to previous record
If currentPosition > 0 Then
currentPosition -= 1
[Link] = currentPosition
End If
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles [Link]
[Link]()
[Link]()
End Sub
Private Sub dgvCRUD_CellContentClick(sender As Object, e As
DataGridViewCellEventArgs) Handles [Link]
Dim selectedrow As DataGridViewRow
selectedrow = [Link]([Link])
[Link] = [Link](0).Value
[Link] = [Link](1).Value
[Link] = [Link](2).Value
[Link] = [Link](3).Value
[Link] = [Link](4).Value
[Link] = [Link](5).Value
End Sub
Private Function DoesStudentIDExist(studentID As String) As Boolean
Dim exists As Boolean = False
Try
' Create the SQL command to check for existing StudentID
cmd = New OleDbCommand("SELECT COUNT(*) FROM FORM3 WHERE StudentID
= @StudentID", con)
[Link]("@StudentID", studentID)
[Link]()
Dim count As Integer = Convert.ToInt32([Link]())
exists = (count > 0)
Catch ex As Exception
[Link]("An error occurred while checking for existing Student ID: " &
[Link])
Finally
If [Link] = [Link] Then
[Link]()
End If
End Try
Return exists
End Function
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles [Link]
' Check if the txtSearch textbox is empty
If [Link]([Link]) Then
' Reload all data into the DataGridView
LoadData()
Return
End If
' Query to search for the specific StudentID
Dim query As String = "SELECT * FROM FORM3 WHERE StudentID = @StudentID"
' Create a new DataSet to hold the search results
Dim searchDataset As New DataSet()
Try
' Initialize the adapter with the search query
adapter = New OleDbDataAdapter(query, con)
' Add the parameter for StudentID
[Link]("@StudentID", [Link])
' Fill the search results into the dataset
[Link](searchDataset, "FORM3")
' Check if any rows were returned
If [Link]("FORM3").[Link] > 0 Then
' Bind the search results to the DataGridView
[Link] = [Link]("FORM3")
Else
[Link]("No student found with the given Student ID.", "No Results",
[Link], [Link])
' Optionally reload all data if no results are found
LoadData()
End If
Catch ex As Exception
[Link]("An error occurred while searching: " & [Link], "Error",
[Link], [Link])
End Try
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles [Link]
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link]()
[Link] = -1
[Link] = [Link]
LoadData()
End Sub
End Class