i'm using visual studio 2012 and microsoft SQL server 2012 to make a save function.
my coding is like this:
form:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
If Len(Trim(txt_nis.Text)) = 0 Or Len(Trim(txt_nisn.Text)) = 0 Or Len(Trim(txt_namasiswa.Text)) = 0 Or Len(Trim(cmb_kelaminsiswa.Text)) = 0 Or Len(Trim(txt_kotalahir.Text)) = 0 Or Len(Trim(DTP_siswa.Value)) = 0 Or Len(Trim(cmb_agamasiswa.Text)) = 0 Or Len(Trim(txt_beratsiswa.Text)) = 0 Or Len(Trim(txt_tinggisiswa.Text)) = 0 Then
MsgBox("Data Belum Lengkap")
Exit Sub
End If
ClassSiswa.Nis = txt_nis.Text
ClassSiswa.Nisn = txt_nisn.Text
ClassSiswa.Nama_Siswa = txt_namasiswa.Text
ClassSiswa.Jenis_Kelamin = cmb_kelaminsiswa.Text
ClassSiswa.Kota_Lahir = txt_kotalahir.Text
ClassSiswa.Tanggal_Lahir = DTP_siswa.Value
ClassSiswa.Agama = cmb_agamasiswa.Text
ClassSiswa.Berat_Badan = txt_beratsiswa.Text
ClassSiswa.Tinggi_Badan = txt_tinggisiswa.Text
ClassSiswa.RekamData(ClassSiswa.opencon)
MsgBox("Data siswa Berhasil Disimpan")
datagridview()
End Sub
class
Public Shared Sub RekamData(ByVal _Cn As SqlClient.SqlConnection)
Dim sql As SqlClient.SqlCommand
sql = New SqlClient.SqlCommand
sql.Connection = _Cn
sql.CommandType = CommandType.Text
sql.CommandText = "Insert into siswa values('" & Nis & "','" & Nisn & "','" & Nama_Siswa & "','" & Jenis_Kelamin & "','" & Kota_Lahir & "','" & Tanggal_Lahir & "','" & Agama & "','" & Berat_Badan & "','" & Tinggi_Badan & "')"
sql.ExecuteNonQuery()
End Sub
SQL query:
Create Table siswa
(
Nis varchar (40) primary key,
Nisn varchar (40),
Nama_Siswa varchar(40),
Jenis_Kelamin varchar (10),
Kota_Lahir varchar (10),
Tanggal_Lahir date,
Agama varchar (10),
Berat_Badan varchar (10),
Tinggi_Badan varchar (10)
)
Nis is primary key.
in the windows form datagridview there is a data that i already saved with Nis : 123, if i saved another data with the same Nis : 123 the program will stop and give me a error because there is already Nis with the value 123.
what i wanted to do is:
instead of show me the error, i want to make a coding that show me a messagebox "data already saved" so that the program won't stopped beacuse of error.
how do i make a coding to do that?