This practical demonstrates how to accept employee details such as Employee Number, Name, and Salary using a VB.NET Windows Forms application, store the data into a database, and display the stored records using a GridView control. It helps students understand database connectivity and data presentation in VB.NET.
Slip-1 Q.B) Write a VB.Net program to accept the details of Employee (ENO, EName, Salary) and store them into the database and display them in a GridView control. 25 Marks
Answer:
Algorithm / Logic
- Start the application.
- Design a Windows Form with TextBoxes for Employee Number (ENO), Employee Name (EName), and Salary, along with Save and Display buttons.
- Create a database table to store employee details.
- Establish a connection between the VB.NET application and the database using a connection string.
- When the Save button is clicked:
- Read the values entered in the TextBoxes.
- Execute an SQL
INSERTquery to store the employee details into the database.
- When the Display button is clicked:
- Execute an SQL
SELECTquery to retrieve all employee records. - Bind the retrieved data to the GridView control.
- Execute an SQL
- Display the employee details in the GridView.
- Stop the application.
Source Code (VB.NET)
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\student\Documents\Emp1.accdb")
Dim adpt As New OleDbDataAdapter("Select * from Emp", con)
Dim ds As New DataSet
Dim cmd As New OleDbCommand
Public Function display()
adpt.Fill(ds, "Emp")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Emp"
Return ds
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
display()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = "INSERT INTO Emp (ENO, EName, Salary) VALUES (" & TextBox1.Text & ", '" & TextBox2.Text & "', " & TextBox3.Text & ")"
con.Open()
cmd.ExecuteNonQuery()
con.Close()
ds.Clear()
display()
End Sub
End Class
FAQ Section
Q1. What is the purpose of a GridView control?
A GridView control is used to display data in tabular form, making it easy to view and manage multiple records from a database.
Q2. Which database can be used for this program?
MS Access, SQL Server, or any other supported database can be used with VB.NET.
Q3. Why is a connection string required?
A connection string is needed to establish communication between the VB.NET application and the database.
Q4. Can this program be extended further?
Yes, additional features such as Update, Delete, and Search operations can be added.
Q5. What concepts are learned from this program?
Students learn form design, database connectivity, SQL queries, and data binding using GridView control.