Vb.net Project With Coding Info
If dataTable.Rows.Count = 0 Then MessageBox.Show("No students found.", "Search Result", MessageBoxButtons.OK, MessageBoxIcon.Information) End If End Sub
Imports System.Data.SqlClient Module DatabaseHelper ' Change the connection string based on your SQL Server instance Public connectionString As String = "Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=StudentDB;Integrated Security=True"
MessageBox.Show("Student added successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information) ClearFields() LoadAllStudents() End If End Sub
Public Function GetConnection() As SqlConnection Return New SqlConnection(connectionString) End Function End Module Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load LoadAllStudents() End Sub Vb.net Project With Coding
If ValidateInputs() Then Dim query As String = "UPDATE Students SET Name=@Name, Age=@Age, Course=@Course WHERE StudentID=@StudentID"
dgvStudents.DataSource = dataTable
If String.IsNullOrWhiteSpace(txtCourse.Text) Then MessageBox.Show("Course is required.", "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Warning) Return False End If If dataTable
' Load all students into DataGridView Private Sub LoadAllStudents() Dim query As String = "SELECT StudentID, Name, Age, Course FROM Students" Dim dataTable As New DataTable()
Private Sub ClearFields() txtStudentID.Clear() txtName.Clear() numAge.Value = 18 txtCourse.Clear() txtName.Focus() End Sub
' Clear all input fields Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click ClearFields() End Sub Vb.net Project With Coding
' Search by Name Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click Dim searchValue As String = txtSearch.Text.Trim()
Using conn As SqlConnection = GetConnection() Using cmd As New SqlCommand(query, conn) cmd.Parameters.AddWithValue("@Name", txtName.Text.Trim()) cmd.Parameters.AddWithValue("@Age", numAge.Value) cmd.Parameters.AddWithValue("@Course", txtCourse.Text.Trim())