' Validate input fields Private Function ValidateInputs() As Boolean If String.IsNullOrWhiteSpace(txtName.Text) Then MessageBox.Show("Name is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) Return False End If
Using conn As SqlConnection = GetConnection() Using adapter As New SqlDataAdapter(query, conn) adapter.Fill(dataTable) End Using End Using
Dim result As DialogResult = MessageBox.Show("Are you sure you want to delete this student?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
' Search by Name Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click Dim searchValue As String = txtSearch.Text.Trim() Vb.net Project With Coding
' Add Student Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click If ValidateInputs() Then Dim query As String = "INSERT INTO Students (Name, Age, Course) VALUES (@Name, @Age, @Course)"
' Load selected row from DataGridView to textboxes Private Sub dgvStudents_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvStudents.CellClick If e.RowIndex >= 0 Then Dim row As DataGridViewRow = dgvStudents.Rows(e.RowIndex) txtStudentID.Text = row.Cells("StudentID").Value.ToString() txtName.Text = row.Cells("Name").Value.ToString() numAge.Value = Convert.ToInt32(row.Cells("Age").Value) txtCourse.Text = row.Cells("Course").Value.ToString() End If End Sub
If String.IsNullOrEmpty(searchValue) Then LoadAllStudents() Return End If ' Validate input fields Private Function ValidateInputs() As
MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) ClearFields() LoadAllStudents() End If End Sub
Dim query As String = "SELECT StudentID, Name, Age, Course FROM Students WHERE Name LIKE @Search + '%'" Dim dataTable As New DataTable()
Private Sub ClearFields() txtStudentID.Clear() txtName.Clear() numAge.Value = 18 txtCourse.Clear() txtName.Focus() End Sub Course) VALUES (@Name
MessageBox.Show("Student deleted successfully.", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information) ClearFields() LoadAllStudents() End If End Sub
| Control Type | Name | Text / Purpose | |--------------|------|----------------| | Label | - | "Student Management System" (Font: bold) | | Label | - | "Student ID" | | TextBox | txtStudentID | (ReadOnly for auto-generated ID) | | Label | - | "Name" | | TextBox | txtName | | | Label | - | "Age" | | NumericUpDown | numAge | Minimum=1, Maximum=100 | | Label | - | "Course" | | TextBox | txtCourse | | | Button | btnAdd | Add Student | | Button | btnUpdate | Update | | Button | btnDelete | Delete | | Button | btnClear | Clear Fields | | TextBox | txtSearch | Search box | | Button | btnSearch | Search | | DataGridView | dgvStudents | (Dock: Bottom, Anchor: Top,Bottom,Left,Right) | Create a new module DatabaseHelper.vb :
' Load all students into DataGridView Private Sub LoadAllStudents() Dim query As String = "SELECT StudentID, Name, Age, Course FROM Students" Dim dataTable As New DataTable()
dgvStudents.DataSource = dataTable
conn.Open() cmd.ExecuteNonQuery() conn.Close() End Using End Using