Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2.  
  3. Public Class FormUser
  4.  
  5.  
  6. Private Sub FormUser_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  7. Dim perintah As New SqlCommand
  8. Dim da As New SqlDataAdapter
  9. Dim ds As New DataSet
  10.  
  11. KoneksiSql = BuatKoneksi()
  12. KoneksiSql.Open()
  13. perintah = KoneksiSql.CreateCommand
  14.  
  15. perintah.CommandText = "SELECT * FROM TUSER"
  16.  
  17. da.SelectCommand = perintah
  18. da.Fill(ds, "Tuser")
  19.  
  20. dgv_user.DataSource = ds
  21. dgv_user.DataMember = "TUser"
  22.  
  23.  
  24.  
  25. End Sub
  26.  
  27. Private Sub btn_tambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_tambah.Click
  28. Dim user, nama, pass, status As String
  29. Dim perintah As New SqlCommand
  30. Dim da As New SqlDataAdapter
  31. Dim ds As New DataSet
  32.  
  33. user = txt_user.Text
  34. nama = txt_nama.Text
  35. pass = txt_pass.Text
  36. status = cb_status.Text
  37.  
  38. KoneksiSql = BuatKoneksi()
  39. KoneksiSql.Open()
  40. perintah = KoneksiSql.CreateCommand
  41.  
  42. perintah.CommandText = "INSERT INTO Tuser values('" & user & "', '" & nama & "', '" & pass & "' , '" & status & "')"
  43. perintah.ExecuteNonQuery()
  44.  
  45. MsgBox("Data berhasil disimpan", MsgBoxStyle.Information, "*")
  46.  
  47. txt_nama.Text = ""
  48. txt_pass.Text = ""
  49. txt_user.Text = ""
  50. cb_status.Text = Nothing
  51.  
  52. FormUser_Load(Me, New System.EventArgs)
  53.  
  54. End Sub
  55.  
  56. Private Sub btn_hapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_hapus.Click
  57. Dim id, jawab As String
  58. Dim perintah As New SqlCommand
  59. Dim da As New SqlDataAdapter
  60. Dim ds As New DataSet
  61. Dim baris As Integer
  62.  
  63. baris = dgv_user.CurrentRow.Index
  64. id = dgv_user.Item(0, baris).Value
  65.  
  66. KoneksiSql = BuatKoneksi()
  67. KoneksiSql.Open()
  68. perintah = KoneksiSql.CreateCommand
  69.  
  70. jawab = MsgBox("AApakah anda yakin", MsgBoxStyle.YesNo, "Konfirmasi")
  71.  
  72. If jawab = vbYes Then
  73. perintah.CommandText = "DELETE FROM Tuser where IDUser = '" & id & "'"
  74. perintah.ExecuteNonQuery()
  75.  
  76. MsgBox("Data berhasil dihapus")
  77.  
  78. FormUser_Load(Me, New System.EventArgs)
  79.  
  80.  
  81. End If
  82.  
  83. End Sub
  84.  
  85.  
  86.  
  87. Private Sub btn_edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_edit.Click
  88. Dim baris As Integer
  89. Dim id As String
  90. Dim perintah As New SqlCommand
  91.  
  92. baris = dgv_user.CurrentRow.Index
  93.  
  94. id = dgv_user.Item(0, baris).Value
  95.  
  96. KoneksiSql = BuatKoneksi()
  97. KoneksiSql.Open()
  98. perintah = KoneksiSql.CreateCommand
  99.  
  100. perintah.CommandText = "SELECT IDuser,NamaUser,Password,Status FROM Tuser where IDuser = '" & id & "'"
  101.  
  102. Dim dr As SqlDataReader
  103. dr = perintah.ExecuteReader
  104. dr.Read()
  105.  
  106. txt_user.Text = dr(0)
  107. txt_nama.Text = dr(1)
  108. txt_pass.Text = dr(2)
  109. cb_status.Text = dr(3)
  110. txt_user.Enabled = False
  111. btn_hapus.Enabled = False
  112. btn_tambah.Enabled = False
  113. End Sub
  114.  
  115.  
  116. Private Sub Simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Simpan.Click
  117. Dim id, nama, job, pass As String
  118. Dim perintah As New SqlCommand
  119.  
  120. id = txt_user.Text
  121. nama = txt_nama.Text
  122. pass = txt_pass.Text
  123. job = cb_status.Text
  124.  
  125. KoneksiSql = BuatKoneksi()
  126. KoneksiSql.Open()
  127. perintah = KoneksiSql.CreateCommand
  128.  
  129. perintah.CommandText = "UPDATE TUser set IDUser = '" & id & "' , NamaUser = '" & nama & "' , Password = '" & pass & "' , Status = '" & job & "' WHERE IDUser = '" & id & "'"
  130. perintah.ExecuteNonQuery()
  131. MsgBox("Data Berhasil Diubah")
  132.  
  133. txt_user.Text = ""
  134. txt_nama.Text = ""
  135. txt_pass.Text = ""
  136. cb_status.Text = Nothing
  137. txt_user.Enabled = True
  138. btn_hapus.Enabled = True
  139. btn_tambah.Enabled = True
  140.  
  141.  
  142. FormUser_Load(Me, New System.EventArgs)
  143. End Sub
  144. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement