Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Data.OleDb
- Public Class Form2
- Private IdJur As Integer = 0
- Private KodeJur As String = String.Empty
- Private Sub btnLanjut_Click(sender As Object, e As EventArgs) Handles btnLanjut.Click
- ' Periksa apakah jurusan telah dipilih
- Dim jurusan As String = cbJurusan.SelectedItem?.ToString()
- ' Ambil jurusan yang dipilih
- If String.IsNullOrWhiteSpace(jurusan) Then
- MessageBox.Show("Harap isi semua data yang diperlukan sebelum melanjutkan.", "Data Tidak Lengkap", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- Return
- End If
- ' Buka Form3 dan kirim nilai UKT
- Dim form3 As New Form3()
- form3.Show()
- Me.Hide()
- End Sub
- Private Sub btnBatal_Click(sender As Object, e As EventArgs) Handles btnBatal.Click
- Application.Exit()
- End Sub
- Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.Click
- ' Ambil jurusan yang dipilih
- Dim jurusanminat As String = cbJurusan.SelectedItem?.ToString()
- If String.IsNullOrWhiteSpace(jurusanminat) Then
- MessageBox.Show("Harap isi semua data yang diperlukan sebelum melanjutkan.", "Data Tidak Lengkap", MessageBoxButtons.OK, MessageBoxIcon.Warning)
- Return
- End If
- GetIdFromJurusan(jurusanminat)
- ' Mendapatkan tanggal dan waktu saat ini
- Dim currentDateTime As DateTime = DateTime.Now
- ' Memformat tanggal dan waktu menjadi format "yyyyMMddHHmmss"
- Dim timestamp As String = currentDateTime.ToString("yyyyMMddHHmmss")
- Dim NoPendataftaran = KodeJur & timestamp
- Using conn As New OleDbConnection(connectionString)
- Try
- ' Buka koneksi
- conn.Open()
- ' Query untuk menyimpan data ke tabel table_calon_mahasiswa
- 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) " &
- "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)"
- Using cmd As New OleDbCommand(query, conn)
- ' Menambahkan parameter untuk query
- cmd.Parameters.AddWithValue("@Nama", nama)
- cmd.Parameters.AddWithValue("@Tempat_Lahir", tempatLahir)
- cmd.Parameters.AddWithValue("@Tanggal_Lahir", tanggalLahir)
- cmd.Parameters.AddWithValue("@Jenis_Kelamin", gender)
- cmd.Parameters.AddWithValue("@Agama", agama)
- cmd.Parameters.AddWithValue("@Asal_Sekolah", asalSekolah)
- cmd.Parameters.AddWithValue("@No_HP", noHP)
- cmd.Parameters.AddWithValue("@Alamat", alamat)
- cmd.Parameters.AddWithValue("@Email", email)
- cmd.Parameters.AddWithValue("@Nama_Ayah", namaAyah)
- cmd.Parameters.AddWithValue("@Nama_Ibu", namaIbu)
- cmd.Parameters.AddWithValue("@No_HP_Orangtua", noHPO)
- cmd.Parameters.AddWithValue("@No_Pendaftaran", NoPendataftaran)
- cmd.Parameters.AddWithValue("@ID_Jurusan", IdJur)
- ' Eksekusi query
- cmd.ExecuteNonQuery()
- btnLanjut.Enabled = True
- btnSimpan.Enabled = False
- End Using
- ' Tampilkan pesan sukses
- MessageBox.Show("Data Mahasiswa Baru berhasil disimpan.", "Data Tersimpan", MessageBoxButtons.OK, MessageBoxIcon.Information)
- Catch ex As Exception
- MessageBox.Show("Error saat menyimpan data: " & ex.Message, "Kesalahan", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Using
- MessageBox.Show("Data Mahasiswa Baru:" & vbCrLf &
- "No Pendaftaran: " & NoPendataftaran, "Data Tersimpan")
- btnSimpan.Enabled = False
- btnLanjut.Enabled = True
- End Sub
- Public Sub GetIdFromJurusan(jurusanMinat As String)
- ' Gantilah path ke database Access Anda
- ' SQL query untuk mengambil ID berdasarkan jurusan
- Dim query As String = "SELECT ID, Kode_Jurusan, UKT FROM jurusan WHERE Jurusan = @jurusan"
- ' Membuat koneksi dan command
- Using connection As New OleDbConnection(connectionString)
- Using command As New OleDbCommand(query, connection)
- ' Menambahkan parameter untuk menghindari SQL Injection
- command.Parameters.AddWithValue("@jurusan", jurusanMinat)
- Try
- ' Membuka koneksi ke database
- connection.Open()
- ' Eksekusi query dan ambil hasilnya
- Using reader As OleDbDataReader = command.ExecuteReader()
- ' Mengecek apakah ada data yang ditemukan
- If reader.HasRows Then
- While reader.Read()
- ' Ambil nilai ID dan Kode_Jurusan dari hasil query
- IdJur = reader.GetInt32(reader.GetOrdinal("ID"))
- KodeJur = reader.GetString(reader.GetOrdinal("Kode_Jurusan"))
- UktTerpilih = reader.GetDecimal(reader.GetOrdinal("UKT"))
- End While
- 'MessageBox.Show("ID Jurusan: " & IdJur & vbCrLf & "Kode Jurusan: " & KodeJur)
- Else
- MessageBox.Show("Jurusan 'Engineering' tidak ditemukan.")
- End If
- End Using
- Catch ex As Exception
- MessageBox.Show("Terjadi kesalahan: " & ex.Message)
- End Try
- End Using
- End Using
- End Sub
- Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Using connection As New OleDbConnection(connectionString)
- ' Create a command to select data from the table
- Dim query As String = "SELECT * FROM jurusan"
- Dim command As New OleDbCommand(query, connection)
- Try
- ' Open the connection
- connection.Open()
- ' Execute the command and read the data
- Dim reader As OleDbDataReader = command.ExecuteReader()
- ' Clear ComboBox to avoid duplications
- cbJurusan.Items.Clear()
- ' Loop through the data and add it to ComboBox
- While reader.Read()
- cbJurusan.Items.Add(reader("jurusan").ToString())
- End While
- ' Close the reader
- reader.Close()
- Catch ex As Exception
- MessageBox.Show("Error: " & ex.Message)
- Finally
- ' Close the connection
- connection.Close()
- End Try
- End Using
- End Sub
- Private Sub Form2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
- Application.Exit()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement