alexa-tracking

Main Content

1024
1024
KASKUS
51
244
https://www.kaskus.co.id/thread/52d7acb30d8b4613258b493d/help-microsoft-visual-studio-2012
[HELP] Microsoft Visual Studio 2012
Gan ane buat software bel sekolah otomatis untuk sekolah tempat ane, tapi ada yang eror. Yaitu jika edit nama guru semua datanya ikut teredit semua.
Ini program yang ane buat
Spoiler for Program:

Mohon bantuannya ya gan
emoticon-Sundul Gan (S)emoticon-Sundul Gan (S)emoticon-Sundul Gan (S)
emoticon-Sundul Gan (S)emoticon-Sundul Gan (S)emoticon-Sundul Gan (S)
ane males download nya gan hahahaha

paling masalah update database nya tuh
gak di kasi where atau where nya salah
Buat pertanyaan yg spesifik gan. Tunjukin fungsi atau sql yg digunakan.
Ini gan yang masih ada erornya

[HELP] Microsoft Visual Studio 2012
Yang menjadi permasalahan jika tombol hapus di klik maka semua isi data basenya ikut terhapus, begitu pula di ja di ubah maka semua ini data basenya jadi sama semua.
untuk codingnya seperti ini
Quote:Imports System.Data
Imports System.Data.OleDb
Public Class frmdata_jadwal_pelajaran
Dim Jadwal_Pelajaran As String
Dim SQL As String
Sub Data_Jadwal_pelajaran()
Try
Call Koneksi()
Tabel = New Data.OleDb.OleDbDataAdapter("select * from TblJadwalPelajaran", Database)
Data = New DataSet
Tabel.Fill(Data)
Record_Data_Jadwal_Pelajaran.DataSource = Data
Record_Data_Jadwal_Pelajaran.DataMember = Data.Tables(0).ToString
DataGridView1.DataSource = Record_Data_Jadwal_Pelajaran
DataGridView1.Columns(0).Width = 50
DataGridView1.Columns(1).Width = 50
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Sub Atur()
btntambah.Enabled = True
btnhapus.Enabled = False
btnubah.Enabled = False
cmbguru.Text = ""
cmbWaktu.Text = ""
CmbHari.Text = ""
cmbkelas.Text = ""
cmbmatapelajaran.Text = ""
Call Koneksi()
Call Data_Jadwal_pelajaran()
End Sub
Private Sub frmdata_jadwal_pelajaran_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Atur()
CmbHari.Items.Add("Senin")
CmbHari.Items.Add("Selasa")
CmbHari.Items.Add("Rabu")
CmbHari.Items.Add("Kamis")
CmbHari.Items.Add("Jum'at")
CmbHari.Items.Add("Sabtu")

Dim sql As String
sql = "select Pelajaran from TblMataPelajaran"
DML = New OleDbCommand(sql, Database)
Cari = DML.ExecuteReader
Do While Cari.Read
cmbmatapelajaran.Items.Add(Cari("Pelajaran"))
Loop

Dim ss As String
ss = "select Jam from TblBell"
DML = New OleDbCommand(ss, Database)
Cari = DML.ExecuteReader
Do While Cari.Read
cmbWaktu.Items.Add(Cari("Jam"))
Loop

Dim sq As String
sq = "select Nama_Kelas from TblDataKelas"
DML = New OleDbCommand(sq, Database)
Cari = DML.ExecuteReader
Do While Cari.Read
cmbkelas.Items.Add(Cari("Nama_Kelas"))
Loop

Dim sl As String
sl = "select Nama_Guru from TblGuru"
DML = New OleDbCommand(sl, Database)
Cari = DML.ExecuteReader
Do While Cari.Read
cmbguru.Items.Add(Cari("Nama_Guru"))
Loop
End Sub
Private Sub btntutup_Click(sender As Object, e As EventArgs) Handles btntutup.Click
Me.Close()
End Sub
Private Sub btntambah_Click(sender As Object, e As EventArgs) Handles btntambah.Click
Try
Call Koneksi()
DML.Connection = Database
DML.CommandType = CommandType.Text
DML.CommandText = "insert into TblJadwalPelajaran values ('" & CmbHari.Text & "', '" & cmbWaktu.Text & "', '" & cmbkelas.Text & "', '" & cmbmatapelajaran.Text & "', '" & cmbguru.Text & "')"
DML.ExecuteNonQuery()
MessageBox.Show("Data Telah Disimpan", "Penyimpanan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub btnhapus_Click(sender As Object, e As EventArgs) Handles btnhapus.Click
Try
Call Koneksi()
DML.Connection = Database
DML.CommandType = CommandType.Text
DML.CommandText = "Delete from TblJadwalPelajaran where Hari = '" & CmbHari.Text & "' and Waktu = '" & cmbWaktu.Text & "'"
DML.ExecuteNonQuery()
MessageBox.Show("Data Telah Dihapus", "Penghapusan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub btnubah_Click(sender As Object, e As EventArgs) Handles btnubah.Click
Try
Call Koneksi()
DML.Connection = Database
DML.CommandType = CommandType.Text
DML.CommandText = "Update TblJadwalPelajaran set Hari = '" & CmbHari.Text & "', Waktu = '" & cmbWaktu.Text & "', Kelas = '" & cmbkelas.Text & "', Mata_Pelajaran = '" & cmbmatapelajaran.Text & "', Guru = '" & cmbguru.Text & "' where Hari = '" & Jadwal_Pelajaran & "'"
DML.ExecuteNonQuery()
MessageBox.Show("Data Telah Diubah", "Pengubahan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Call Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Try
CmbHari.Text = DataGridView1.SelectedCells(0).Value
cmbWaktu.Text = DataGridView1.SelectedCells(1).Value
cmbkelas.Text = DataGridView1.SelectedCells(2).Value
cmbguru.Text = DataGridView1.SelectedCells(3).Value
cmbmatapelajaran.Text = DataGridView1.SelectedCells(4).Value
Jadwal_Pelajaran = DataGridView1.SelectedCells(0).Value
btntambah.Enabled = False
btnhapus.Enabled = True
btnubah.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

End Class

Agar jika di ubah/di hapus tidak berubah semua caranya gimana ya gan? Jadi jika di ubah/dihapus data yang dipilih saja yang berubah
Untuk data basenya seperti ini
[HELP] Microsoft Visual Studio 2012
coba check deh...

"Update TblJadwalPelajaran set Hari = '" & CmbHari.Text & "', Waktu = '" & cmbWaktu.Text & "', Kelas = '" & cmbkelas.Text & "', Mata_Pelajaran = '" & cmbmatapelajaran.Text & "', Guru = '" & cmbguru.Text & "' where Hari = '" & Jadwal_Pelajaran & "'"

coba bikin query "select * from TblJadwalPelajaran where Hari = '" & Jadwal_Pelajaran & "'"

ntar bisa diliat dari query select tersebut, semua hari atau hanya hari yang ditentuin doang...

atau coba bikin messagebox / alert untuk liat string dari query ente... sesuai dengan yang ente harapkan nggak....

tambahan....
sebenernya bisa diakalin dengan membuat satu textfield hidden buat nama guru sebelumnya misalnya 'namaguruold' jadi querynya ditambahin kayak gini....

"Update TblJadwalPelajaran set Hari = '" & CmbHari.Text & "', Waktu = '" & cmbWaktu.Text & "', Kelas = '" & cmbkelas.Text & "', Mata_Pelajaran = '" & cmbmatapelajaran.Text & "', Guru = '" & cmbguru.Text & "' where Hari = '" & Jadwal_Pelajaran & "' and Guru = '" & namaguruold.Text & "'"

Quote:Original Posted By wasitnya.mantap
coba check deh...

"Update TblJadwalPelajaran set Hari = '" & CmbHari.Text & "', Waktu = '" & cmbWaktu.Text & "', Kelas = '" & cmbkelas.Text & "', Mata_Pelajaran = '" & cmbmatapelajaran.Text & "', Guru = '" & cmbguru.Text & "' where Hari = '" & Jadwal_Pelajaran & "'"

coba bikin query "select * from TblJadwalPelajaran where Hari = '" & Jadwal_Pelajaran & "'"

ntar bisa diliat dari query select tersebut, semua hari atau hanya hari yang ditentuin doang...

atau coba bikin messagebox / alert untuk liat string dari query ente... sesuai dengan yang ente harapkan nggak....

tambahan....
sebenernya bisa diakalin dengan membuat satu textfield hidden buat nama guru sebelumnya misalnya 'namaguruold' jadi querynya ditambahin kayak gini....

"Update TblJadwalPelajaran set Hari = '" & CmbHari.Text & "', Waktu = '" & cmbWaktu.Text & "', Kelas = '" & cmbkelas.Text & "', Mata_Pelajaran = '" & cmbmatapelajaran.Text & "', Guru = '" & cmbguru.Text & "' where Hari = '" & Jadwal_Pelajaran & "' and Guru = '" & namaguruold.Text & "'"


kalo pake textfield hidden apakah harus merubah data basenya gan? kalo data basenya berubah, kira-kira jadi seperti apa?
Up... Up...
Sebaiknya agan pakai semacam primary key untuk menandakan record tersebut.
Quick fix saja untuk code agan, bisa pakai composite key, tapi itu pun ada kelemahannya -- bila ada record yang persis sama.
Coba pelajari code di bawah.
Code:

Imports System.Data.OleDb
Imports System.Linq

Public Class frmdata_jadwal_pelajaran
Private Const ReadJadwalPelajaranSql As System.String = "SELECT * FROM TblJadwalPelajaran"
Private Const ReadMataPelajaranSql As System.String = "SELECT Pelajaran FROM TblMataPelajaran"
Private Const ReadBellSql As System.String = "SELECT Jam FROM TblBell"
Private Const ReadDataKelasSql As System.String = "SELECT Nama_Kelas FROM TblDataKelas"
Private Const ReadSelectGuruSql As System.String = "SELECT Nama_Guru FROM TblGuru"

Private Const TableMataPelajaranColumn As System.String = "Pelajaran"
Private Const TableBellColumn As System.String = "Jam"
Private Const TableDataKelasColumn As System.String = "Nama_Kelas"
Private Const TableGuruColumn As System.String = "Nama_Guru"

Private ReadOnly _updateSql As System.String =
<sql>
UPDATE
TblJadwalPelajaran
SET
Hari = @NewHari,
Waktu = @NewWaktu,
Kelas = @NewKelas,
Mata_Pelajaran = @NewMataPelajaran,
Guru = @NewGuru
WHERE
Hari = @CurrentHari AND
Waktu = @CurrentWaktu AND
Kelas = @CurrentKelas AND
Mata_Pelajaran = @CurrentMataPelajaran AND
Guru = @CurrentGuru
</sql>.Value.Trim()

Private ReadOnly _deleteSql As System.String =
<sql>
DELETE FROM
TblJadwalPelajaran
WHERE
Hari = @CurrentHari AND
Waktu = @CurrentWaktu AND
Kelas = @CurrentKelas AND
Mata_Pelajaran = @CurrentMataPelajaran AND
Guru = @CurrentGuru
</sql>.Value.Trim()

Private ReadOnly _createSql As System.String =
<sql>
INSERT INTO
TblJadwalPelajaran
VALUES
(
@NewHari,
@NewWaktu,
@NewKelas,
@NewMataPelajaran,
@NewGuru
)
</sql>.Value.Trim()

Private _hari As System.String
Private _waktu As System.String
Private _kelas As System.String
Private _mataPelajaran As System.String
Private _guru As System.String

Private Sub frmdata_jadwal_pelajaran_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load
InitCombo()
Atur()
End Sub

Private Sub btntutup_Click(sender As System.Object, e As EventArgs) Handles btntutup.Click
CmbHari.DataSource = Nothing
cmbmatapelajaran.DataSource = Nothing
cmbWaktu.DataSource = Nothing
cmbkelas.DataSource = Nothing
cmbguru.DataSource = Nothing
DataGridView1.DataSource = Nothing
Record_Data_Jadwal_Pelajaran.DataSource = Nothing
Close()
End Sub

Private Sub btntambah_Click(sender As System.Object, e As EventArgs) Handles btntambah.Click
Try
If (IsAllComboBoxTextNotEmpty()) Then
Throw New ArgumentException("All ComboBox must be Selected")
End If
Koneksi()
Using cmd As OleDbCommand = Database.CreateCommand()
With cmd
.CommandText = _createSql
.Parameters.AddWithValue("@NewHari", CmbHari.Text)
.Parameters.AddWithValue("@NewWaktu", cmbWaktu.Text)
.Parameters.AddWithValue("@NewKelas", cmbkelas.Text)
.Parameters.AddWithValue("@NewMataPelajaran", cmbmatapelajaran.Text)
.Parameters.AddWithValue("@NewGuru", cmbguru.Text)
.ExecuteNonQuery()
End With
End Using
Database.Close()
Database.Dispose()
MessageBox.Show("Data Telah Disimpan", "Penyimpanan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub btnhapus_Click(sender As System.Object, e As EventArgs) Handles btnhapus.Click
Try
Koneksi()
Using cmd As OleDbCommand = Database.CreateCommand()
With cmd
.CommandText = _deleteSql
.Parameters.AddWithValue("@CurrentHari", _hari)
.Parameters.AddWithValue("@CurrentWaktu", _waktu)
.Parameters.AddWithValue("@CurrentKelas", _kelas)
.Parameters.AddWithValue("@CurrentMataPelajaran", _mataPelajaran)
.Parameters.AddWithValue("@CurrentGuru", _guru)
.ExecuteNonQuery()
End With
End Using
Database.Close()
Database.Dispose()
MessageBox.Show("Data Telah Dihapus", "Penghapusan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub btnubah_Click(sender As System.Object, e As EventArgs) Handles btnubah.Click
Try
If (IsAllComboBoxTextNotEmpty()) Then
Throw New ArgumentException("All ComboBox must be Selected")
End If
Koneksi()
Using cmd As OleDbCommand = Database.CreateCommand()
With cmd
.CommandText = _updateSql
.Parameters.AddWithValue("@NewHari", CmbHari.Text)
.Parameters.AddWithValue("@NewWaktu", cmbWaktu.Text)
.Parameters.AddWithValue("@NewKelas", cmbkelas.Text)
.Parameters.AddWithValue("@NewMataPelajaran", cmbmatapelajaran.Text)
.Parameters.AddWithValue("@NewGuru", cmbguru.Text)
.Parameters.AddWithValue("@CurrentHari", _hari)
.Parameters.AddWithValue("@CurrentWaktu", _waktu)
.Parameters.AddWithValue("@CurrentKelas", _kelas)
.Parameters.AddWithValue("@CurrentMataPelajaran", _mataPelajaran)
.Parameters.AddWithValue("@CurrentGuru", _guru)
.ExecuteNonQuery()
End With
End Using
Database.Close()
Database.Dispose()
MessageBox.Show("Data Telah Diubah", "Pengubahan Sukses", MessageBoxButtons.OK, MessageBoxIcon.Information)
Atur()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub DataGridView1_CellDoubleClick(sender As System.Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Try
With DataGridView1
_hari = .CurrentRow.Cells(0).Value
_waktu = .CurrentRow.Cells(1).Value
_kelas = .CurrentRow.Cells(2).Value
_mataPelajaran = .CurrentRow.Cells(3).Value
_guru = .CurrentRow.Cells(4).Value
End With
CmbHari.Text = _hari
cmbWaktu.Text = _waktu
cmbkelas.Text = _kelas
cmbmatapelajaran.Text = _mataPelajaran
cmbguru.Text = _guru
btntambah.Enabled = False
btnhapus.Enabled = True
btnubah.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub InitCombo()
CmbHari.DataSource = New Globalization.CultureInfo("id-ID").DateTimeFormat.DayNames
cmbmatapelajaran.DisplayMember = TableMataPelajaranColumn
cmbWaktu.DisplayMember = TableBellColumn
cmbkelas.DisplayMember = TableDataKelasColumn
cmbguru.DisplayMember = TableGuruColumn
Koneksi()
Using ds As DataSet = New DataSet()
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
da.SelectCommand = New OleDbCommand(ReadMataPelajaranSql, Database)
da.Fill(ds, TableMataPelajaranColumn)
da.SelectCommand = New OleDbCommand(ReadBellSql, Database)
da.Fill(ds, TableBellColumn)
da.SelectCommand = New OleDbCommand(ReadDataKelasSql, Database)
da.Fill(ds, TableDataKelasColumn)
da.SelectCommand = New OleDbCommand(ReadSelectGuruSql, Database)
da.Fill(ds, TableGuruColumn)
cmbmatapelajaran.DataSource = ds.Tables(TableMataPelajaranColumn)
cmbWaktu.DataSource = ds.Tables(TableBellColumn)
cmbkelas.DataSource = ds.Tables(TableDataKelasColumn)
cmbguru.DataSource = ds.Tables(TableGuruColumn)
End Using
Database.Close()
Database.Dispose()
End Sub

Private Sub Atur()
btntambah.Enabled = True
btnhapus.Enabled = False
btnubah.Enabled = False
cmbguru.Text = System.String.Empty
cmbWaktu.Text = System.String.Empty
CmbHari.Text = System.String.Empty
cmbkelas.Text = System.String.Empty
cmbmatapelajaran.Text = System.String.Empty
Data_Jadwal_pelajaran()
End Sub

Private Sub Data_Jadwal_pelajaran()
Try
Koneksi()
Using ds As DataSet = New DataSet()
Dim da As OleDbDataAdapter = New OleDbDataAdapter(ReadJadwalPelajaranSql, Database)
da.Fill(ds)
With Record_Data_Jadwal_Pelajaran
.DataSource = Nothing
.DataSource = ds
.DataMember = ds.Tables(0).ToString()
End With
With DataGridView1
.DataSource = Record_Data_Jadwal_Pelajaran
.Columns(0).Width = 50
.Columns(1).Width = 50
End With
End Using
Database.Close()
Database.Dispose()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Function IsAllComboBoxTextNotEmpty() As System.Boolean
Dim cmb = New ComboBox()
Return Controls.Cast(Of Control)().Any(Function(o) (o.GetType() Is cmb.GetType()) AndAlso (o.Text.Equals(System.String.Empty)))
End Function
End Class