Advertisement
arlendafitranto

Form2

Nov 7th, 2024 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.35 KB | Source Code | 0 0
  1. Imports System.Data.OleDb
  2.  
  3.  
  4. Public Class Form2
  5.     Private IdJur As Integer = 0
  6.     Private KodeJur As String = String.Empty
  7.     Private Sub btnLanjut_Click(sender As Object, e As EventArgs) Handles btnLanjut.Click
  8.         ' Periksa apakah jurusan telah dipilih
  9.         Dim jurusan As String = cbJurusan.SelectedItem?.ToString()
  10.  
  11.         ' Ambil jurusan yang dipilih
  12.         If String.IsNullOrWhiteSpace(jurusan) Then
  13.             MessageBox.Show("Harap isi semua data yang diperlukan sebelum melanjutkan.", "Data Tidak Lengkap", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  14.             Return
  15.         End If
  16.  
  17.  
  18.         ' Buka Form3 dan kirim nilai UKT
  19.         Dim form3 As New Form3()
  20.         form3.Show()
  21.         Me.Hide()
  22.     End Sub
  23.     Private Sub btnBatal_Click(sender As Object, e As EventArgs) Handles btnBatal.Click
  24.         Application.Exit()
  25.     End Sub
  26.  
  27.     Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
  28.  
  29.  
  30.         ' Ambil jurusan yang dipilih
  31.         Dim jurusanminat As String = cbJurusan.SelectedItem?.ToString()
  32.  
  33.         If String.IsNullOrWhiteSpace(jurusanminat) Then
  34.             MessageBox.Show("Harap isi semua data yang diperlukan sebelum melanjutkan.", "Data Tidak Lengkap", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  35.             Return
  36.         End If
  37.  
  38.         GetIdFromJurusan(jurusanminat)
  39.  
  40.         ' Mendapatkan tanggal dan waktu saat ini
  41.         Dim currentDateTime As DateTime = DateTime.Now
  42.  
  43.         ' Memformat tanggal dan waktu menjadi format "yyyyMMddHHmmss"
  44.         Dim timestamp As String = currentDateTime.ToString("yyyyMMddHHmmss")
  45.         Dim NoPendataftaran = KodeJur & timestamp
  46.  
  47.         Using conn As New OleDbConnection(connectionString)
  48.             Try
  49.                 ' Buka koneksi
  50.                 conn.Open()
  51.  
  52.                 ' Query untuk menyimpan data ke tabel table_calon_mahasiswa
  53.                 Dim query As String = "INSERT INTO calon_mahasiswa (Nama, Tempat_Lahir, Tanggal_Lahir, Jenis_Kelamin, Agama, Asal_Sekolah, No_HP, Alamat, Email, Nama_Ayah, Nama_Ibu, No_HP_Orangtua, No_Pendaftaran, ID_Jurusan) " &
  54.                           "VALUES (@Nama, @Tempat_Lahir, @Tanggal_Lahir, @Jenis_Kelamin, @Agama, @Asal_Sekolah, @No_HP, @Alamat, @Email, @Nama_Ayah, @Nama_Ibu, @No_HP_Orangtua, @No_Pendaftaran, @ID_Jurusan)"
  55.  
  56.                 Using cmd As New OleDbCommand(query, conn)
  57.                     ' Menambahkan parameter untuk query
  58.                     cmd.Parameters.AddWithValue("@Nama", nama)
  59.                     cmd.Parameters.AddWithValue("@Tempat_Lahir", tempatLahir)
  60.                     cmd.Parameters.AddWithValue("@Tanggal_Lahir", tanggalLahir)
  61.                     cmd.Parameters.AddWithValue("@Jenis_Kelamin", gender)
  62.                     cmd.Parameters.AddWithValue("@Agama", agama)
  63.                     cmd.Parameters.AddWithValue("@Asal_Sekolah", asalSekolah)
  64.                     cmd.Parameters.AddWithValue("@No_HP", noHP)
  65.                     cmd.Parameters.AddWithValue("@Alamat", alamat)
  66.                     cmd.Parameters.AddWithValue("@Email", email)
  67.                     cmd.Parameters.AddWithValue("@Nama_Ayah", namaAyah)
  68.                     cmd.Parameters.AddWithValue("@Nama_Ibu", namaIbu)
  69.                     cmd.Parameters.AddWithValue("@No_HP_Orangtua", noHPO)
  70.                     cmd.Parameters.AddWithValue("@No_Pendaftaran", NoPendataftaran)
  71.                     cmd.Parameters.AddWithValue("@ID_Jurusan", IdJur)
  72.                     ' Eksekusi query
  73.                     cmd.ExecuteNonQuery()
  74.  
  75.                     btnLanjut.Enabled = True
  76.                     btnSimpan.Enabled = False
  77.                 End Using
  78.  
  79.                 ' Tampilkan pesan sukses
  80.                 MessageBox.Show("Data Mahasiswa Baru berhasil disimpan.", "Data Tersimpan", MessageBoxButtons.OK, MessageBoxIcon.Information)
  81.  
  82.             Catch ex As Exception
  83.                 MessageBox.Show("Error saat menyimpan data: " & ex.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
  84.             End Try
  85.         End Using
  86.  
  87.  
  88.         MessageBox.Show("Data Mahasiswa Baru:" & vbCrLf &
  89.              "No Pendaftaran: " & NoPendataftaran, "Data Tersimpan")
  90.  
  91.         btnSimpan.Enabled = False
  92.         btnLanjut.Enabled = True
  93.     End Sub
  94.     Public Sub GetIdFromJurusan(jurusanMinat As String)
  95.         ' Gantilah path ke database Access Anda
  96.  
  97.         ' SQL query untuk mengambil ID berdasarkan jurusan
  98.         Dim query As String = "SELECT ID, Kode_Jurusan, UKT FROM jurusan WHERE Jurusan = @jurusan"
  99.  
  100.         ' Membuat koneksi dan command
  101.         Using connection As New OleDbConnection(connectionString)
  102.             Using command As New OleDbCommand(query, connection)
  103.                 ' Menambahkan parameter untuk menghindari SQL Injection
  104.                 command.Parameters.AddWithValue("@jurusan", jurusanMinat)
  105.  
  106.                 Try
  107.                     ' Membuka koneksi ke database
  108.                     connection.Open()
  109.  
  110.                     ' Eksekusi query dan ambil hasilnya
  111.                     Using reader As OleDbDataReader = command.ExecuteReader()
  112.                         ' Mengecek apakah ada data yang ditemukan
  113.                         If reader.HasRows Then
  114.                             While reader.Read()
  115.                                 ' Ambil nilai ID dan Kode_Jurusan dari hasil query
  116.                                 IdJur = reader.GetInt32(reader.GetOrdinal("ID"))
  117.                                 KodeJur = reader.GetString(reader.GetOrdinal("Kode_Jurusan"))
  118.                                 UktTerpilih = reader.GetDecimal(reader.GetOrdinal("UKT"))
  119.                             End While
  120.                             'MessageBox.Show("ID Jurusan: " & IdJur & vbCrLf & "Kode Jurusan: " & KodeJur)
  121.                         Else
  122.                             MessageBox.Show("Jurusan 'Engineering' tidak ditemukan.")
  123.                         End If
  124.                     End Using
  125.  
  126.                 Catch ex As Exception
  127.                     MessageBox.Show("Terjadi kesalahan: " & ex.Message)
  128.                 End Try
  129.             End Using
  130.         End Using
  131.  
  132.     End Sub
  133.     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  134.  
  135.         Using connection As New OleDbConnection(connectionString)
  136.             ' Create a command to select data from the table
  137.             Dim query As String = "SELECT * FROM jurusan"
  138.             Dim command As New OleDbCommand(query, connection)
  139.  
  140.             Try
  141.                 ' Open the connection
  142.                 connection.Open()
  143.  
  144.                 ' Execute the command and read the data
  145.                 Dim reader As OleDbDataReader = command.ExecuteReader()
  146.  
  147.                 ' Clear ComboBox to avoid duplications
  148.                 cbJurusan.Items.Clear()
  149.  
  150.                 ' Loop through the data and add it to ComboBox
  151.                 While reader.Read()
  152.  
  153.                     cbJurusan.Items.Add(reader("jurusan").ToString())
  154.  
  155.                 End While
  156.  
  157.                 ' Close the reader
  158.                 reader.Close()
  159.             Catch ex As Exception
  160.                 MessageBox.Show("Error: " & ex.Message)
  161.             Finally
  162.                 ' Close the connection
  163.                 connection.Close()
  164.             End Try
  165.         End Using
  166.     End Sub
  167.  
  168.  
  169.     Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
  170.         Application.Exit()
  171.     End Sub
  172.  
  173. End Class
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement