Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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.
Posted

Why are you doing this?
VB
DTP_siswa.Value = Date.Now.ToString("dd-MM-yyyy")

Assuming that DTP_siswa is your DateTimePicker, then the Value property is a DateTime value, but Date.Now is a VB6 function, which doesn't return anything particularly useful in a .Net context.
And why format a date value as a string in order to set the value of a DateTime at all? If you do, the system then had to parse that to convert it to a "real" DateTime anyway.

Try this:
VB
DTP_siswa.Value = DateTime.Now

And then look at the rest of your code.
 
Share this answer
 
Comments
asasql 28-Mar-15 3:26am    
i did it. but i can't change the date in date time picker after i select the date, Also now there is another error, When click the button save,there is an error in class student "sql exception was unhandled" how do i solve this?

i'm sorry that i forgot to ask about the button save and post the sql query i used in sql server 2012 before.

sql script:
Create Database KPIRWAN
use KPIRWAN

Create Table siswa
(
Nis int primary key,
Nisn int,
Nama_Siswa varchar(40),
Jenis_Kelamin varchar (10),
Kota_Lahir varchar (10),
Tanggal_Lahir date,
Agama varchar (10),
Berat_Badan int,
Tinggi_Badan int)

select*from siswa
drop table siswa
Drop database KPIRWAN
OriginalGriff 28-Mar-15 6:08am    
That's a different question, and needs to be raised as a new question rather than added to an existing one.

But...you do realize that that SQL will do nothing useful at all, and will never return any rows?
Why are you creating a new database, adding a table, selecting records from the new (and very, very empty) table, and then deleting the database again?
DateTimePicker's Value property is of DateTime type.
So why are you trying to assign it a string? That's the first problem.
You should never handle datetimes with strings. Only use a string representation of the datetime when you want to present it to the user. Internally, storage and computations should stick to DateTime type.

Then, there is another problem: when the ValueChanged event is fired, you just try to replace the selected value with the current date/time (DateTime.Now). Are you sure it is really what you want to do? What is the point of having a DateTimePicker if you just replace the selected value with the current time? You should get the selected value in a DateTime variable and try to do something with it.
 
Share this answer
 
Comments
asasql 28-Mar-15 3:46am    
thank you very much, that was my mistake.the program is running, But after i input all the data and click the button save the message box that says "data berhasil di simpan" is displayed, But the data that was saved wasn't displayed on the datagridview or the sql server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900