Hello, i am a beginner at programming and i want to ask how to make this program i am making right now work.
Currently for my college assignment i was asked to make a application to register new students at a middle school using visual studio 2012 and microsoft sql server 2012.
this is the code i'm currently working on:
form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ClassKoneksi.namadatabase = "KPIRWAN"
ClassSiswa.namadatabase = "KPIRWAN"
Dim dsdata As New DataSet
Dim sql As String
sql = "select*from datasiswa"
dsdata = ClassSiswa.displayData(ClassSiswa.opencon, sql, "Datasiswa")
DataGridView1.DataSource = dsdata
DataGridView1.DataMember = "datasiswa"
DataGridView1.ReadOnly = True
ClassSiswa.closecon()
End Sub
Private Sub btnKeluar_Click(sender As Object, e As EventArgs) Handles btnKeluar.Click
Me.Close()
End Sub
Private Sub btnSimpan_Click(sender As Object, e As EventArgs) Handles btnSimpan.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")
End Sub
Private Sub DateTimePicker_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DTP_siswa.ValueChanged
DTP_siswa.Format = DateTimePickerFormat.Custom
DTP_siswa.ShowUpDown = True
DTP_siswa.Value = Date.Now.ToString("dd-MM-yyyy")
End Sub
End Class
my question is:
when i was running the program with f5 and changing the date time picker to another date the program stopped and said "InvalidCastException was unhandled" in the blocked line above. I don't know where the error is, i keep trying to fix it but it didn't work.
is the error in the form coding, class coding, or the SQL server?
-----------------------------------------------------------------------------------
here are the coding of the class:
class connection:
Public Class ClassKoneksi
Private Shared CN As SqlClient.SqlConnection
Private Shared _NamaDatabase As String
Public Shared Property namadatabase As String
Get
Return _NamaDatabase
End Get
Set(value As String)
_NamaDatabase = value
End Set
End Property
Public Shared Function opencon() As SqlClient.SqlConnection
CN = New SqlClient.SqlConnection
CN.ConnectionString = "Data Source=RICKY-PC\LENOVOSQL;Initial Catalog=KPIRWAN;Integrated Security=True "
CN.Open()
Return CN
End Function
Public Shared Sub closecon()
CN.Close()
CN.ConnectionString = Nothing
End Sub
class student:
Public Class ClassSiswa
Inherits ClassKoneksi
Private Shared _Nis, _Nisn, _Berat_Badan, _Tinggi_Badan As Integer
Private Shared _Nama_Siswa, _Jenis_Kelamin, _Kota_Lahir, _Agama As String
Private Shared _Tanggal_Lahir As Date
Public Shared Property Nis() As Integer
Get
Return _Nis
End Get
Set(ByVal value As Integer)
_Nis = value
End Set
End Property
Public Shared Property Nisn() As Integer
Get
Return _Nisn
End Get
Set(ByVal value As Integer)
_Nisn = value
End Set
End Property
Public Shared Property Berat_Badan() As Integer
Get
Return _Berat_Badan
End Get
Set(ByVal value As Integer)
_Berat_Badan = value
End Set
End Property
Public Shared Property Tinggi_Badan() As Integer
Get
Return _Tinggi_Badan
End Get
Set(ByVal value As Integer)
_Tinggi_Badan = value
End Set
End Property
Public Shared Property Nama_Siswa() As String
Get
Return _Nama_Siswa
End Get
Set(ByVal value As String)
_Nama_Siswa = value
End Set
End Property
Public Shared Property Jenis_Kelamin() As String
Get
Return _Jenis_Kelamin
End Get
Set(ByVal value As String)
_Jenis_Kelamin = value
End Set
End Property
Public Shared Property Kota_Lahir() As String
Get
Return _Kota_Lahir
End Get
Set(ByVal value As String)
_Kota_Lahir = value
End Set
End Property
Public Shared Property Tanggal_Lahir() As Date
Get
Return _Tanggal_Lahir
End Get
Set(ByVal value As Date)
_Tanggal_Lahir = value
End Set
End Property
Public Shared Property Agama() As String
Get
Return _Agama
End Get
Set(ByVal value As String)
_Agama = value
End Set
End Property
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 datasiswa values('" & Nis & "','" & Nisn & "','" & Nama_Siswa & "','" & Jenis_Kelamin & "','" & Kota_Lahir & "','" & Tanggal_Lahir & "','" & Agama & "','" & Berat_Badan & "','" & Tinggi_Badan & "')"
sql.ExecuteNonQuery()
End Sub
Public Shared Function displayData(ByVal _cn As SqlClient.SqlConnection, ByVal _sql As String, ByVal _datasiswa As String) As DataSet
Dim _da As New SqlClient.SqlDataAdapter
Dim _dsdata As New DataSet
Dim sql As New SqlClient.SqlCommand
_dsdata.Clear()
sql.Connection = _cn
sql.CommandType = CommandType.Text
sql.CommandText = _sql
_da.SelectCommand = sql
_da.Fill(_dsdata, _datasiswa)
Return _dsdata
End Function
End Class
i'm sorry for the inconvenience but,i'm really stressed out for the past few days trying to figure this thing out.