Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Imports System.Data
  3. Imports System.IO
  4.  
  5. Public Class Form1
  6. Dim conn As MySqlConnection
  7. Dim SQL As String
  8. Dim s As Integer = 0
  9. Dim d As Path
  10.  
  11. Private Sub Form1_Load(ByVal sender As Object, _
  12. ByVal e As System.EventArgs) Handles Me.Load
  13.  
  14. Data_Load()
  15.  
  16. End Sub
  17. Sub Data_Load()
  18. Dim myCommand As New MySqlCommand
  19. Dim myAdapter As New MySqlDataAdapter
  20. Dim myData As New DataTable
  21.  
  22. conn = New MySqlConnection()
  23. conn.ConnectionString = "server=localhost;user id=root;" & _
  24. "password=;database=inventaris;"
  25. Try
  26. If conn.State = ConnectionState.Closed Then conn.Open()
  27. SQL = "Select no_pol, type, user, tahun_pembuatan, tanggal_terbit, pajak_berakhir From mobil"
  28.  
  29. myCommand.Connection = conn
  30. myCommand.CommandText = SQL
  31.  
  32. myAdapter.SelectCommand = myCommand
  33. myAdapter.Fill(myData)
  34. With grdData
  35. .DataSource = myData
  36. .AllowUserToAddRows = False
  37. .AllowUserToDeleteRows = False
  38. .ReadOnly = True
  39.  
  40. .Columns(0).HeaderText = "NOMOR POLISI"
  41. .Columns(1).HeaderText = "TYPE"
  42. .Columns(2).HeaderText = "USER"
  43. .Columns(3).HeaderText = "TAHUN PEMBUATAN"
  44. .Columns(4).HeaderText = "TANGGAL TERBIT STNK"
  45. .Columns(5).HeaderText = "PAJAK BERAKHIR STNK"
  46.  
  47.  
  48. .Columns(0).Width = 100
  49. .Columns(1).Width = 150
  50. .Columns(2).Width = 150
  51. .Columns(3).Width = 100
  52. .Columns(4).Width = 100
  53. .Columns(5).Width = 100
  54.  
  55. End With
  56.  
  57.  
  58. conn.Close()
  59. Catch myerror As MySqlException
  60. MessageBox.Show("Error: " & myerror.Message)
  61. Finally
  62. conn.Dispose()
  63. End Try
  64. End Sub
  65.  
  66.  
  67.  
  68. Private Sub tbrSave_Click(ByVal sender As System.Object, _
  69. ByVal e As System.EventArgs) Handles tbrSave.Click
  70. Dim myCommand As New MySqlCommand
  71.  
  72. conn = New MySqlConnection()
  73. conn.ConnectionString = "server=localhost;user id=root;" & _
  74. "password=;database=inventaris"
  75. Try
  76. conn.Open()
  77. If tbrEdit.Enabled = True Then
  78. SQL = "INSERT INTO mobil (no_pol, type, user, tahun_pembuatan,tanggal_terbit,pajak_berakhir) VALUES " & _
  79. "('" & txtNoPol.Text & "', '" & txtType.Text & "', '" & txtUser.Text & "', '" & txtTahunPembuatan.Text & "', '" & txtTanggalTerbit.Text & "', '" & txtPajakBerakhir.Text & "')"
  80. Else
  81. SQL = "UPDATE mobil SET type='" & txtType.Text & "', user='" & txtUser.Text & "',tahun_pembuatan='" & txtTahunPembuatan.Text & "',tanggal_terbit='" & txtTanggalTerbit.Text & "',pajak_berakhir='" & txtPajakBerakhir.Text & "'WHERE no_pol='" & txtNoPol.Text & "'"
  82. End If
  83. myCommand.Connection = conn
  84. myCommand.CommandText = SQL
  85. myCommand.ExecuteNonQuery()
  86.  
  87.  
  88. If tbrEdit.Enabled = True Then
  89. MsgBox("Data baru tersimpan")
  90. Else
  91. MsgBox("Perubahan tersimpan")
  92. End If
  93.  
  94. tbrCancel_Click(Nothing, Nothing)
  95.  
  96. conn.Close()
  97. Catch myerror As MySqlException
  98. MessageBox.Show("Error: " & myerror.Message)
  99. Finally
  100. conn.Dispose()
  101. End Try
  102. End Sub
  103.  
  104. Private Sub grdData_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles grdData.CellMouseDoubleClick
  105.  
  106.  
  107. txtNoPol.Text = grdData.CurrentRow.Cells(0).Value
  108. txtType.Text = grdData.CurrentRow.Cells(1).Value
  109. txtUser.Text = grdData.CurrentRow.Cells(2).Value
  110. txtTahunPembuatan.Text = grdData.CurrentRow.Cells(3).Value
  111. txtTanggalTerbit.Text = grdData.CurrentRow.Cells(4).Value
  112. txtPajakBerakhir.Text = grdData.CurrentRow.Cells(5).Value
  113.  
  114. tbrEdit.Enabled = False
  115. txtNoPol.ReadOnly = True
  116.  
  117. End Sub
  118.  
  119. Private Sub tbrEdit_Click(ByVal sender As System.Object, _
  120. ByVal e As System.EventArgs) Handles tbrEdit.Click
  121.  
  122. grdData_CellMouseDoubleClick(Nothing, Nothing)
  123.  
  124. End Sub
  125.  
  126. Private Sub tbrCancel_Click(ByVal sender As System.Object, _
  127. ByVal e As System.EventArgs) Handles tbrCancel.Click
  128. txtNoPol.Text = String.Empty
  129. txtType.Text = String.Empty
  130. txtUser.Text = String.Empty
  131. txtTahunPembuatan.Text = String.Empty
  132. txtTanggalTerbit.Text = String.Empty
  133. txtPajakBerakhir.Text = String.Empty
  134.  
  135. tbrEdit.Enabled = True
  136. txtNoPol.ReadOnly = False
  137. Data_Load()
  138. End Sub
  139.  
  140. Private Sub tbrDelete_Click(ByVal sender As System.Object, _
  141. ByVal e As System.EventArgs) Handles tbrDelete.Click
  142.  
  143. If MsgBox("Yakin akan menghapus data?", MsgBoxStyle.YesNo, _
  144. "Konfirmasi") = MsgBoxResult.No Then Exit Sub
  145.  
  146. Dim myCommand As New MySqlCommand
  147.  
  148. conn = New MySqlConnection()
  149. conn.ConnectionString = "server=localhost;user id=root;" & _
  150. "password=;database=inventaris"
  151. Try
  152. conn.Open()
  153. SQL = "DELETE FROM mobil WHERE no_pol = " & _
  154. "'" & grdData.CurrentRow.Cells(0).Value & "'"
  155.  
  156. myCommand.Connection = conn
  157. myCommand.CommandText = SQL
  158. myCommand.ExecuteNonQuery()
  159.  
  160. MsgBox("Data terhapus")
  161.  
  162. tbrCancel_Click(Nothing, Nothing)
  163.  
  164. conn.Close()
  165. Catch myerror As MySqlException
  166. MessageBox.Show("Error: " & myerror.Message)
  167. Finally
  168. conn.Dispose()
  169. End Try
  170. End Sub
  171.  
  172.  
  173.  
  174.  
  175. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement