You are on page 1of 3

Insert update and delete operation of database

Design Form

Source Code

Imports System.Data.SqlClient
Public Class Form2
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim flag As String
Dim intempid As Integer
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnsave.Click
Try
cmd = con.CreateCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = "insert into tblemp(name,city,mobileno,designation,doj)
values('" & txtemp.Text & "','" & txtcity.Text & "','" & txtmobileno.Text & "','" &
cmbdesignation.SelectedItem & "','" & DateTimePicker1.Value & "') "
cmd.ExecuteNonQuery()
MsgBox("Record is saved")
dispdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
clearcontrols()
End Sub
Sub clearcontrols()
txtemp.Text = ""
txtcity.Text = ""
txtmobileno.Text = ""
txtemp.Focus()
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load

Try
con.ConnectionString = "Data
Source=.\SQLEXPRESS;AttachDbFilename=c:\users\administrator\documents\visual studio
2010\Projects\WindowsApplication1\WindowsApplication1\dbstudent.mdf;Integrated
Security=True;User Instance=True"
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
dispdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub btnall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles btnall.Click
flag = "all"
dispdata()
End Sub
Sub dispdata()
Try
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
If flag = "search" Then
cmd.CommandText = "select * from tblemp where city='" & txtscity.Text &
"'"
Else
cmd.CommandText = "select * from tblemp"
End If
cmd.ExecuteNonQuery()
Dim dt As New DataTable()
Dim da As New SqlDataAdapter(cmd)
da.fill(dt)
DataGridView1.DataSource = dt
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnsearch.Click
flag = "search"
dispdata()
End Sub

Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
intempid = DataGridView1.Rows(e.RowIndex).Cells(0).Value
txtemp.Text = DataGridView1.Rows(e.RowIndex).Cells(1).Value
txtcity.Text = DataGridView1.Rows(e.RowIndex).Cells(2).Value
txtmobileno.Text = DataGridView1.Rows(e.RowIndex).Cells(3).Value
'cmbdesignation.SelectedItem = DataGridView1.Rows(e.RowIndex).Cells(4).Value
cmbdesignation.Text = DataGridView1.Rows(e.RowIndex).Cells(4).Value
DateTimePicker1.Value = DataGridView1.Rows(e.RowIndex).Cells(5).Value
End Sub

Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btnupdate.Click
Try
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "update tblemp set name='" & txtemp.Text & "',city='" &
txtcity.Text & "',mobileno='" & txtmobileno.Text & "',designation='" &
cmbdesignation.SelectedItem & "' where empid=" & intempid & " "
cmd.ExecuteNonQuery()
MsgBox("Record Is SuccessFully updated")
dispdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
clearcontrols()
End Sub

Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles btndelete.Click
Try
cmd = con.CreateCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = "delete from tblemp where empid=" & intempid & " "
cmd.ExecuteNonQuery()
MsgBox("Record Is SuccessFully deleted")
dispdata()
Catch ex As Exception
MsgBox(ex.Message)
End Try
clearcontrols()
End Sub
End Class

You might also like