Imports System.Data.
SqlClient
Public Class EmployeeManagementSystem
' Connection String for the database (modify as per your database
configuration)
Dim connectionString As String = "Data Source=YourServerName;Initial
Catalog=YourDatabaseName;Integrated Security=True"
' Method to execute SQL queries
Private Function ExecuteQuery(query As String) As Boolean
Dim connection As New SqlConnection(connectionString)
Try
connection.Open()
Dim command As New SqlCommand(query, connection)
command.ExecuteNonQuery()
Return True
Catch ex As Exception
MessageBox.Show("Error executing query: " & ex.Message)
Return False
Finally
connection.Close()
End Try
End Function
' Method to add a new employee
Private Sub AddEmployee(name As String, age As Integer, designation As String)
Dim query As String = $"INSERT INTO Employees (Name, Age, Designation)
VALUES ('{name}', {age}, '{designation}')"
ExecuteQuery(query)
End Sub
' Method to update employee information
Private Sub UpdateEmployee(id As Integer, name As String, age As Integer,
designation As String)
Dim query As String = $"UPDATE Employees SET Name = '{name}', Age = {age},
Designation = '{designation}' WHERE EmployeeID = {id}"
ExecuteQuery(query)
End Sub
' Method to delete an employee
Private Sub DeleteEmployee(id As Integer)
Dim query As String = $"DELETE FROM Employees WHERE EmployeeID = {id}"
ExecuteQuery(query)
End Sub
' Method to display all employees
Private Sub DisplayEmployees()
Dim query As String = "SELECT * FROM Employees"
Dim connection As New SqlConnection(connectionString)
Dim adapter As New SqlDataAdapter(query, connection)
Dim table As New DataTable()
Try
connection.Open()
adapter.Fill(table)
DataGridView1.DataSource = table
Catch ex As Exception
MessageBox.Show("Error displaying employees: " & ex.Message)
Finally
connection.Close()
End Try
End Sub
' Event handler for Add button
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim name As String = txtName.Text
Dim age As Integer = Convert.ToInt32(txtAge.Text)
Dim designation As String = txtDesignation.Text
AddEmployee(name, age, designation)
DisplayEmployees()
End Sub
' Event handler for Update button
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles
btnUpdate.Click
Dim id As Integer = Convert.ToInt32(txtID.Text)
Dim name As String = txtName.Text
Dim age As Integer = Convert.ToInt32(txtAge.Text)
Dim designation As String = txtDesignation.Text
UpdateEmployee(id, name, age, designation)
DisplayEmployees()
End Sub
' Event handler for Delete button
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles
btnDelete.Click
Dim id As Integer = Convert.ToInt32(txtID.Text)
DeleteEmployee(id)
DisplayEmployees()
End Sub
' Event handler for Form Load
Private Sub EmployeeManagementSystem_Load(sender As Object, e As EventArgs)
Handles MyBase.Load
DisplayEmployees()
End Sub
End Class