You are on page 1of 2

Imports System.Data.

SqlClient

Public Class Form1


Dim query As String = "server=(localdb)\Projects;database=dbprofile"
Dim con As New SqlConnection(query)

Sub loaddata()
Dim query As String = "select * from profile"
Dim adpt As New SqlClient.SqlDataAdapter(query, con)
Dim ds As New DataSet()
adpt.Fill(ds, "Emp")
DataGridView1.DataSource = ds.Tables(0)
con.Close()
txtid.Clear()
txtName.Clear()
txtEmail.Clear()
txtWebsite.Clear()
txtSearch.Clear()
End Sub

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


loaddata()
End Sub

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


btnInput.Click
Dim cmd As SqlCommand
con.Open()
Try
cmd = con.CreateCommand
cmd.CommandText = "Insert into
profile(id,name,email,website)values(@id,@name,@email,@website);"
cmd.Parameters.AddWithValue("@id", txtid.Text)
cmd.Parameters.AddWithValue("@name", txtName.Text)
cmd.Parameters.AddWithValue("@email", txtEmail.Text)
cmd.Parameters.AddWithValue("@website", txtWebsite.Text)
cmd.ExecuteNonQuery()
loaddata()
Catch ex As Exception
End Try
End Sub

Private Sub DataGridView1_CellContentClick_1(sender As Object, e As


DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim row As DataGridViewRow = DataGridView1.CurrentRow
Try
txtid.Text = row.Cells(0).Value.ToString()
txtName.Text = row.Cells(1).Value.ToString()
txtEmail.Text = row.Cells(2).Value.ToString()
txtWebsite.Text = row.Cells(3).Value.ToString()
Catch ex As Exception
End Try
End Sub

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


btnUpdate.Click
Dim cmd As SqlCommand
con.Open()
Try
cmd = con.CreateCommand()
cmd.CommandText = "Update profile set
id=@id,name=@name,email=@email,website=@website"
cmd.Parameters.AddWithValue("@id", txtid.Text)
cmd.Parameters.AddWithValue("@name", txtName.Text)
cmd.Parameters.AddWithValue("@email", txtEmail.Text)
cmd.Parameters.AddWithValue("@website", txtWebsite.Text)
cmd.ExecuteNonQuery()
loaddata()
Catch ex As Exception
End Try
End Sub

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


btnDelete.Click
Dim cmd As SqlCommand
con.Open()
Try
cmd = con.CreateCommand()
cmd.CommandText = "Delete from profile where id=@id;"
cmd.Parameters.AddWithValue("@id", txtid.Text)
cmd.ExecuteNonQuery()
loaddata()
Catch ex As Exception
End Try
End Sub

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


txtSearch.TextChanged
Dim adapter As SqlDataAdapter
Dim ds As New DataSet
Try
con.Open()
adapter = New SqlDataAdapter("Select * from profile where name like '%"
& txtSearch.Text & "%'", con)
adapter.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
con.Close()
txtid.Clear()
txtName.Clear()
txtEmail.Clear()
txtWebsite.Clear()
Catch ex As Exception
End Try
End Sub

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


btnReset.Click
loaddata()
End Sub

End Class

You might also like