Advertisement
null0x4

Untitled

Aug 6th, 2023
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Data.SqlClient
  2. Public Class Data_Instansi
  3.     Dim Conn As SqlConnection
  4.     Dim Da As SqlDataAdapter
  5.     Dim Cmd As SqlCommand
  6.     Dim Rd As SqlDataReader
  7.     Dim Ds As DataSet
  8.     Dim MyDB As String
  9.  
  10.     ' Fungsi untuk melakukan koneksi ke database
  11.    Sub koneksi()
  12.         MyDB = "Data Source=DESKTOP-JIL4I4Q;initial catalog=ptbbm;integrated security=true"
  13.         Conn = New SqlConnection(MyDB)
  14.         If Conn.State = ConnectionState.Closed Then
  15.             Conn.Open()
  16.         End If
  17.     End Sub
  18.  
  19.     ' Fungsi untuk mengosongkan data pada form
  20.    Sub Cleardata()
  21.         DateTimePicker1.Value = Now
  22.         TextBox3.Text = ""
  23.         TextBox4.Text = ""
  24.         TextBox5.Text = ""
  25.         ComboBox1.Text = ""
  26.         TextBox6.Text = ""
  27.         ComboBox3.Text = ""
  28.         ComboBox2.Text = ""
  29.     End Sub
  30.  
  31.     ' Fungsi untuk kondisi awal saat form diload
  32.    Sub kondisiAwal()
  33.         Call koneksi()
  34.         Da = New SqlDataAdapter("SELECT * FROM datapelanggan", Conn)
  35.         Ds = New DataSet
  36.         Ds.Clear()
  37.         DataGridView1.Columns.Clear()
  38.         Da.Fill(Ds, "datapelanggan")
  39.         DataGridView1.DataSource = (Ds.Tables("datapelanggan"))
  40.  
  41.         ' Menambahkan kolom "Delete" ke DataGridView
  42.        Dim col As New DataGridViewButtonColumn
  43.         col.DataPropertyName = "PropertyName"
  44.         col.HeaderText = "Delete"
  45.         col.Name = "delete"
  46.         DataGridView1.Columns.Add(col)
  47.         For i = 0 To DataGridView1.RowCount - 1
  48.             DataGridView1.Rows(i).Cells("delete").Value = "Delete"
  49.         Next
  50.  
  51.         ' Menambahkan kolom "Edit" ke DataGridView
  52.        Dim coledit As New DataGridViewButtonColumn
  53.         coledit.DataPropertyName = "PropertyName"
  54.         coledit.HeaderText = "Edit"
  55.         coledit.Name = "edit"
  56.         DataGridView1.Columns.Add(coledit)
  57.         For i = 0 To DataGridView1.RowCount - 1
  58.             DataGridView1.Rows(i).Cells("edit").Value = "Edit"
  59.         Next
  60.     End Sub
  61.  
  62.     ' Fungsi untuk menampilkan nama jenis instansi dari database ke ComboBox
  63.    Sub MunculNamaJenisInstansi()
  64.         Call koneksi()
  65.         ComboBox1.Items.Clear()
  66.         Try
  67.             Dim Ds As New DataSet
  68.             Dim CB As New SqlDataAdapter("SELECT * FROM jenisinstansi", Conn)
  69.             CB.Fill(Ds, "jenisinstansi")
  70.             Me.ComboBox1.DataSource = Ds.Tables("jenisinstansi")
  71.             Me.ComboBox1.ValueMember = "kode_jenis_instansi"
  72.             Me.ComboBox1.DisplayMember = "nama_jenisinstansi"
  73.         Catch ex As Exception
  74.             ' Tangani eksepsi/kesalahan sesuai kebutuhan Anda
  75.        End Try
  76.     End Sub
  77.  
  78.     ' Fungsi untuk menampilkan nama provinsi dari database ke ComboBox
  79.    Sub MunculNamaProvinsi()
  80.         Call koneksi()
  81.         ComboBox3.Items.Clear()
  82.         Try
  83.             Dim Ds As New DataSet
  84.             Dim CB As New SqlDataAdapter("SELECT * FROM provinsi", Conn)
  85.             CB.Fill(Ds, "provinsi")
  86.             Me.ComboBox3.DataSource = Ds.Tables("provinsi")
  87.             Me.ComboBox3.ValueMember = "kode_provinsi"
  88.             Me.ComboBox3.DisplayMember = "nama_provinsi"
  89.         Catch ex As Exception
  90.             ' Tangani eksepsi/kesalahan sesuai kebutuhan Anda
  91.        End Try
  92.     End Sub
  93.  
  94.     Private Sub Data_Instansi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  95.         ' Memanggil fungsi kondisiAwal saat form diload
  96.        Call kondisiAwal()
  97.         ' Memanggil fungsi MunculNamaJenisInstansi untuk menampilkan nama jenis instansi di ComboBox
  98.        Call MunculNamaJenisInstansi()
  99.         ' Memanggil fungsi MunculNamaProvinsi untuk menampilkan nama provinsi di ComboBox
  100.        Call MunculNamaProvinsi()
  101.     End Sub
  102.  
  103.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  104.         Try
  105.             Call koneksi()
  106.             If TextBox4.Text.Trim() <> "" Then
  107.                 ' Menjalankan query untuk insert data ke database
  108.                Dim InputData As String = "INSERT INTO datapelanggan (tanggal_daftar, no_pelanggan, nama_instansi, nama_instansi_disingkat, kode_jenis_instansi, alamat_instansi, kode_kota_kabupaten) VALUES ('" & DateTimePicker1.Value & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & ComboBox1.SelectedValue & "','" & TextBox6.Text & "','" & ComboBox2.SelectedValue & "')"
  109.                 Cmd = New SqlCommand(InputData, Conn)
  110.                 Cmd.ExecuteNonQuery()
  111.                 MsgBox("Data Berhasil Diinput")
  112.                 ' Memanggil fungsi kondisiAwal untuk refresh DataGridView
  113.                Call kondisiAwal()
  114.                 ' Memanggil fungsi Cleardata untuk mengosongkan data pada form
  115.                Call Cleardata()
  116.             Else
  117.                 MsgBox("Textbox tidak boleh kosong. Silakan masukkan data terlebih dahulu.")
  118.             End If
  119.         Catch ex As Exception
  120.             MsgBox("Terjadi kesalahan saat memasukkan data: " & ex.Message)
  121.         Finally
  122.             Conn.Close()
  123.         End Try
  124.     End Sub
  125.  
  126.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  127.         Try
  128.             Call koneksi()
  129.             If TextBox4.Text.Trim() <> "" Then
  130.                 ' Menjalankan query untuk update data di database
  131.                Dim EditData As String = "UPDATE datapelanggan SET no_pelanggan='" & TextBox3.Text & "', nama_instansi='" & TextBox4.Text & "', nama_instansi_disingkat='" & TextBox5.Text & "', kode_jenis_instansi='" & ComboBox1.SelectedValue & "', alamat_instansi='" & TextBox6.Text & "', tanggal_daftar='" & DateTimePicker1.Value & "' WHERE no_pelanggan='" & TextBox3.Text & "'"
  132.                 Cmd = New SqlCommand(EditData, Conn)
  133.                 Cmd.ExecuteNonQuery()
  134.                 MsgBox("Data Berhasil DiEdit")
  135.                 ' Memanggil fungsi kondisiAwal untuk refresh DataGridView
  136.                Call kondisiAwal()
  137.             Else
  138.                 MsgBox("Isi terlebih dahulu. Agar bisa merubah data.")
  139.             End If
  140.         Catch ex As Exception
  141.             MsgBox("Terjadi kesalahan saat mengedit data: " & ex.Message)
  142.         End Try
  143.     End Sub
  144.  
  145.     Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
  146.         Try
  147.             Da = New SqlDataAdapter("SELECT * FROM Kotakabupaten WHERE kode_provinsi=" & ComboBox3.SelectedValue, Conn)
  148.             Ds = New DataSet
  149.             Ds.Clear()
  150.             Da.Fill(Ds, "Kotakabupaten")
  151.  
  152.             Me.ComboBox2.DataSource = Ds.Tables("Kotakabupaten")
  153.             Me.ComboBox2.ValueMember = "kode_kota_kabupaten"
  154.             Me.ComboBox2.DisplayMember = "nama_kota_kabupaten"
  155.         Catch ex As Exception
  156.         End Try
  157.     End Sub
  158.  
  159.     Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
  160.         Select Case e.ColumnIndex
  161.             Case 4
  162.                 ' Jika tombol "Delete" di-klik, tampilkan dialog konfirmasi dan hapus data jika user mengonfirmasi
  163.                If e.RowIndex >= 0 AndAlso e.RowIndex < DataGridView1.RowCount Then
  164.                     If e.ColumnIndex = DataGridView1.Columns("delete").Index Then
  165.                         Dim kodePelanggan As String = DataGridView1.Rows(e.RowIndex).Cells("no_pelanggan").Value.ToString()
  166.                         If MessageBox.Show("Apakah Anda yakin ingin menghapus data ini?", "Konfirmasi Hapus", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
  167.                             Try
  168.                                 Call koneksi()
  169.                                 Dim hapusData As String = "DELETE FROM datapelanggan WHERE no_pelanggan='" & kodePelanggan & "'"
  170.                                 Cmd = New SqlCommand(hapusData, Conn)
  171.                                 Cmd.ExecuteNonQuery()
  172.                                 MsgBox("Data Berhasil Dihapus")
  173.                                 Call kondisiAwal()
  174.                             Catch ex As Exception
  175.                                 MsgBox("Error menghapus data: " & ex.Message)
  176.                             Finally
  177.                                 Conn.Close()
  178.                             End Try
  179.                         End If
  180.                     End If
  181.                 End If
  182.             Case 3
  183.                 ' Jika tombol "Edit" di-klik, tampilkan data dari row yang di-klik ke TextBox dan ComboBox
  184.                If e.RowIndex >= 0 AndAlso e.RowIndex < DataGridView1.RowCount Then
  185.                     If e.ColumnIndex = DataGridView1.Columns("edit").Index Then
  186.                         TextBox3.Text = DataGridView1.Rows(e.RowIndex).Cells("no_pelanggan").Value.ToString()
  187.                         TextBox4.Text = DataGridView1.Rows(e.RowIndex).Cells("nama_instansi").Value.ToString()
  188.                         TextBox5.Text = DataGridView1.Rows(e.RowIndex).Cells("nama_instansi_disingkat").Value.ToString()
  189.                         ComboBox1.SelectedValue = DataGridView1.Rows(e.RowIndex).Cells("kode_jenis_instansi").Value.ToString()
  190.                         TextBox6.Text = DataGridView1.Rows(e.RowIndex).Cells("alamat_instansi").Value.ToString()
  191.                         DateTimePicker1.Value = CDate(DataGridView1.Rows(e.RowIndex).Cells("tanggal_daftar").Value)
  192.                     End If
  193.                 End If
  194.         End Select
  195.     End Sub
  196. End Class
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement