-----------------Insert & Update & Delete-----------------
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Parth\Documents\emp.accdb")
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cmd As New OleDbCommand("select * from emp", cnn)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
With cbCity.Items
.Add("Surat")
.Add("Navsari")
.Add("Valsad")
.Add("Vapi")
.Add("Bharuch")
.Add("Vadodara")
End With
cbCity.SelectedIndex = 0
End Sub
---------------------------------------------------------------------------------------------------------------
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
Dim cmd As New OleDbCommand("insert into emp values (" & txENo.Text & ",'" & txEname.Text & "','" & cbCity.SelectedItem & "','" & DatePicker.Value.Date & "'," & txDno.Text & "," & txSalary.Text & " )", cnn)
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
Dim cmd1 As New OleDbCommand("select * from emp", cnn)
Dim da1 As New OleDbDataAdapter(cmd1)
Dim ds1 As New DataSet
da1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
End Sub
------------------------------------------------------------------------------------------------------------
Private Sub txENo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txENo.TextChanged
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim cmd As New OleDbCommand("delete from emp where empno =" & Val(txENo.Text), cnn)
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
MsgBox("Record Deleted")
Dim cmd1 As New OleDbCommand("select * from emp", cnn)
Dim da1 As New OleDbDataAdapter(cmd1)
Dim ds1 As New DataSet
da1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
End Sub
-----------------------------------------------------------------------------------------------------------------------
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim cmd As New OleDbCommand("update emp set ename ='" & txEname.Text & "' , city ='" & cbCity.SelectedItem & "' ,doj='" & DatePicker.Value.Date & "',dno=" & txDno.Text & ",sal=" & txSalary.Text & " where empno=" & txENo.Text & "", cnn)
cnn.Open()
cmd.ExecuteNonQuery()
cnn.Close()
MsgBox("Record Updateted")
Dim cmd1 As New OleDbCommand("select * from emp", cnn)
Dim da1 As New OleDbDataAdapter(cmd1)
Dim ds1 As New DataSet
da1.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
End Sub
End Class
0 Comments
Post a Comment