0% found this document useful (0 votes)
48 views5 pages

Frmdatagrid Dataset Datatable Oledbdataadapter Oledbcommand: "Select From Tbemp"

This document contains code for connecting to an Access database and performing CRUD operations on an employee table. It includes classes for displaying data in a datagrid, connecting to the database, and modules for connecting and disconnecting from the database. The datagrid class loads employee data from the database and allows selecting rows to view/edit individual records. The database connection class populates a combo box with employees, and allows adding, deleting, and updating records by executing SQL commands on the database connection. The module defines shared functions for opening and closing the database connection.

Uploaded by

Siddhi Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

Frmdatagrid Dataset Datatable Oledbdataadapter Oledbcommand: "Select From Tbemp"

This document contains code for connecting to an Access database and performing CRUD operations on an employee table. It includes classes for displaying data in a datagrid, connecting to the database, and modules for connecting and disconnecting from the database. The datagrid class loads employee data from the database and allows selecting rows to view/edit individual records. The database connection class populates a combo box with employees, and allows adding, deleting, and updating records by executing SQL commands on the database connection. The module defines shared functions for opening and closing the database connection.

Uploaded by

Siddhi Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Imports System.Data.

OleDb
Public Class frmDataGrid
Dim ds As New DataSet
Dim dt As DataTable
Dim adp As OleDbDataAdapter
Dim cmd As OleDbCommand

Private Sub btnShw_Click(sender As Object, e As EventArgs) Handles btnShw.Click


Dim sSQL1 As String
sSQL1 = "select * from tbEmp"
ds.Clear()
adp = New OleDbDataAdapter(sSQL1, dbcon)
adp.Fill(ds)
dgvEmp.DataSource = ds.Tables(0)
dgvEmp.Columns.Item(0).HeaderText = "Employee Code"
dgvEmp.Columns.Item(1).HeaderText = "Employee Name"
dgvEmp.Columns.Item(2).HeaderText = "Employee Salary"
End Sub

Private Sub frmDataGrid_Load(sender As Object, e As EventArgs) Handles MyBase.Load


Call pConnectDB()
End Sub

Private Sub dgvEmp_CellContentClick(sender As Object, e As


DataGridViewCellEventArgs) Handles dgvEmp.CellContentClick

End Sub

Private Sub dgvEmp_Click(sender As Object, e As EventArgs) Handles dgvEmp.Click


Dim myrow As DataRow
dt = ds.Tables(0)
myrow = dt.Rows(dgvEmp.CurrentRow.Index)
txtCod.Text = myrow("empCod")
txtNam.Text = myrow("empNam")
txtSal.Text = myrow("empSal")
End Sub

Private Sub btnUpdat_Click(sender As Object, e As EventArgs) Handles


btnUpdat.Click
Dim sSQL As String
sSQL = "update tbEmp set empNam=" & "'" & txtNam.Text & "'" & "," & "empSal="
& txtSal.Text & " where empCod=" & txtCod.Text
MsgBox(sSQL)
cmd = New OleDbCommand(sSQL, dbcon)
cmd.ExecuteNonQuery()
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


Dim sSQL1 As String
sSQL1 = " insert into tbEmp values(" & txtCod.Text & ",'" & txtNam.Text & "'"
& "," & txtSal.Text & ")"
MsgBox(sSQL1)
cmd = New OleDbCommand(sSQL1, dbcon)
cmd.ExecuteNonQuery()
End Sub

Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click


Dim sSQL2 As String
sSQL2 = "delete from tbEmp where empCod=" & txtCod.Text
MsgBox(sSQL2)
cmd = New OleDbCommand(sSQL2, dbcon)
cmd.ExecuteNonQuery()
End Sub
End Class

Db connect
Imports System.Data.OleDb
Public Class frmDBconnect
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader 'for method 1 (data reader)
Dim ds As New DataSet ' for method 2 (data adapter)
Dim adp As OleDbDataAdapter
Dim rowno As Integer
Dim totalrows As Integer

Private Sub frmDBconnect_Load(sender As Object, e As EventArgs) Handles


MyBase.Load
Call pConnectDB()
Call pLoadCboEmp()

End Sub
Private Sub pLoadCboEmp()
' cmd = New OleDbCommand("select * from tbEmp", dbcon) 'METHOD 1
' dr = cmd.ExecuteReader()
'While (dr.Read())
'cboEmp.Items.Add(dr.Item("empNam"))
'End While
rowno = 0
adp = New OleDbDataAdapter("select empNam, empCod from tbEmp", dbcon) 'METHOD
2
adp.Fill(ds, "tbEmp")
cboEmp.DataSource = ds.Tables(0)
totalrows = ds.Tables(0).Rows.Count
cboEmp.DisplayMember = "empNam"
cboEmp.ValueMember = "empCod"

End Sub

Private Sub cboEmp_TextChanged(sender As Object, e As EventArgs) Handles


cboEmp.TextChanged
' If cboEmp.SelectedIndex > 0 Then
'txtEmpnam.Text = cboEmp.Text
' txtEmpcod.Text = cboEmp.SelectedValue

' End If

Dim sSQL As String


If cboEmp.SelectedIndex > 0 Then

sSQL = "select * from tbEmp where empCod=" & cboEmp.SelectedValue


MsgBox(sSQL)
cmd = New OleDbCommand(sSQL, dbcon) 'METHOD 1
dr = cmd.ExecuteReader()
dr.Read()
txtEmpSal.Text = dr.Item("empSal")
txtEmpcod.Text = dr.Item("empCod")
txtEmpnam.Text = dr.Item("empNam")
dr.Close()

End If
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


Dim sSQL As String
sSQL = " insert into tbEmp values(" & txtEmpcod.Text & ",'" & txtEmpnam.Text &
"'" & "," & txtEmpSal.Text & ")"
cmd = New OleDbCommand(sSQL, dbcon)
cmd.ExecuteNonQuery()
txtEmpcod.Text = ""
txtEmpnam.Text = ""
txtEmpSal.Text = ""
End Sub

Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click


Dim sSQL As String
' sSQL = "delete from tbEmp where empCod=" & txtEmpcod.Text
'MsgBox(sSQL)
sSQL = "delete from tbEmp where empCod=" & txtEmpcod.Text
MsgBox(sSQL)
cmd = New OleDbCommand(sSQL, dbcon)
cmd.ExecuteNonQuery()
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles


btnUpdate.Click
Dim sSQL As String
sSQL = "update tbEmp set empNam=" & "'" & txtEmpnam.Text & "'" & "," &
"empSal=" & txtEmpSal.Text & " where empNam=" & "'" & cboEmp.Text & "'"
MsgBox(sSQL)
cmd = New OleDbCommand(sSQL, dbcon)
cmd.ExecuteNonQuery()
End Sub

Private Sub txtEmpcod_TextChanged(sender As Object, e As EventArgs) Handles


txtEmpcod.TextChanged

End Sub

Private Sub frmDBconnect_Closed(sender As Object, e As EventArgs) Handles


Me.Closed
Call pDisconnectDB()

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click


Call pDisconnectDB()

End Sub

Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles


btnFirst.Click
rowno = 0
txtEmpcod.Text = ds.Tables("tbEmp").Rows(rowno).Item(1)
txtEmpnam.Text = ds.Tables("tbEmp").Rows(rowno).Item(2)
txtEmpSal.Text = ds.Tables("tbEmp").Rows(rowno).Item(3)
End Sub

Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click


rowno += 1
If totalrows > rowno Then
txtEmpcod.Text = ds.Tables("tbEmp").Rows(rowno).Item(1)
txtEmpnam.Text = ds.Tables("tbEmp").Rows(rowno).Item(2)
' txtEmpSal.Text = ds.Tables("tbEmp").Rows(rowno).Item(3)
Else
rowno -= 1
MessageBox.Show("No more Records")
End If
End Sub

Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles


btnPrevious.Click
rowno -= 1
If rowno >= 0 And totalrows > rowno Then
txtEmpcod.Text = ds.Tables("tbEmp").Rows(rowno).Item(1)
txtEmpnam.Text = ds.Tables("tbEmp").Rows(rowno).Item(2)
txtEmpSal.Text = ds.Tables("tbEmp").Rows(rowno).Item(3)
Else
rowno += 1
MessageBox.Show("No more Records")
End If
End Sub

Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click


rowno = totalrows - 1
txtEmpcod.Text = ds.Tables("tbEmp").Rows(rowno).Item(1)
txtEmpnam.Text = ds.Tables("tbEmp").Rows(rowno).Item(2)
txtEmpSal.Text = ds.Tables("tbEmp").Rows(rowno).Item(3)

End Sub
End Class

MOD
Imports System.Data.OleDb
Module modConnectDB
Public dbcon As New OleDbConnection
Public Sub pConnectDB()
dbcon.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=E:\vb.net 6105\WindowsApplication7\WindowsApplication7\dbEmp.accdb"
dbcon.Open()
' MsgBox("connection successful")
End Sub
Public Sub pDisconnectDB()
If dbcon.State = ConnectionState.Open Then
dbcon.Close()

End If
End Sub
End Module

You might also like