Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Imports System.Data.

SqlClient
Public Class Form1
Dim str As String
Dim con As SqlConnection
Function checkkd(str As String) As Boolean
Dim query As String = "select count(*) from tbl_pelanggan where kd ='" &
txtkd.Text & "'"
Dim cmd As New SqlCommand(query, con)
Return cmd.ExecuteScalar()
End Function
Sub koneksi()
Try
str = "data source =ALLWIN7PC-PC;initial catalog =tetx;integrated
security=true"
con = New SqlConnection(str)
con.Open()

Catch ex As Exception

End Try

End Sub

Private Sub btninsert_Click(sender As Object, e As EventArgs) Handles btninsert.Click


Try
If txtkd.Text = "" Then
MessageBox.Show("kd belum da")
Else
If checkkd(txtkd.Text) = False Then
Dim cmd As New SqlCommand("insert into
tbl_pelanggan(kd,nama,alamat,contact,ket) values ('" & txtkd.Text & "','" & txtnama.Text
& "','" & txtalamat.Text & "','" & txtcontact.Text & "','" & txtket.Text & "')", con)
cmd.ExecuteNonQuery()
MessageBox.Show("INSERTTED")
txtalamat.Text = ""
txtcontact.Text = ""
txtkd.Text = ""
txtket.Text = ""
txtnama.Text = ""

Else
MsgBox("tdkbisa")
End If
End If

Catch ex As Exception

End Try

End Sub

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

Call koneksi()
End Sub
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Try
If checkkd(txtkd.Text) Then
Dim cmd As New SqlCommand("delete from tbl_pelanggan where kd = '" &
txtkd.Text & "'", con)
cmd.ExecuteNonQuery()
MessageBox.Show("deleted")
txtalamat.Text = ""
txtcontact.Text = ""
txtkd.Text = ""
txtket.Text = ""
txtnama.Text = ""
Else
MsgBox("undel")

End If
Catch ex As Exception

End Try

End Sub

Private Sub btnupdate_Click(sender As Object, e As EventArgs) Handles btnupdate.Click


Try
If checkkd(txtkd.Text) Then
Dim cmd As New SqlCommand("update tbl_pelanggan set nama ='" &
txtnama.Text & "',alamat ='" & txtalamat.Text & "',contact ='" & txtcontact.Text &
"',ket='" & txtket.Text & "' where kd ='" & txtkd.Text & "'", con)
cmd.ExecuteNonQuery()
MsgBox("updated")
txtalamat.Text = ""
txtcontact.Text = ""
txtkd.Text = ""
txtket.Text = ""
txtnama.Text = ""
Else
MsgBox("unupdated")
End If
Catch ex As Exception

End Try

End Sub
End Class

You might also like