visual basic 2012 OOP方法中的更新函数



我正在visual studio 2012中为更新函数编写代码,但出现了"invalidcastexception was unhandled"错误。

这是windows形式的更新功能的编码:

Private Sub btnupdate_Click(sender As Object, e As EventArgs) Handles btnupdate.Click
ClassSiswa.Nis = txt_nis.Text
ClassSiswa.Nisn = txt_nisn.Text
ClassSiswa.Jenis_Kelamin = cmb_kelaminsiswa.Text
ClassSiswa.Kota_Lahir = txt_kotalahir.Text
ClassSiswa.Tanggal_Lahir = DTP_siswa.Text
ClassSiswa.Agama = cmb_agamasiswa.Text
ClassSiswa.Berat_Badan = txt_beratsiswa.Text
ClassSiswa.Tinggi_Badan = txt_tinggi_badan.Text
ClassSiswa.EditData(ClassSiswa.opencon, txt_nis.Text)
MessageBox.Show(" Data Telah Diupdate")
ClassKoneksi.closecon()
datagridview()
End Sub

这是函数的类:

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 EditData(ByVal _cn As SqlClient.SqlConnection, ByVal Nis As Integer)
Dim sql As New SqlClient.SqlCommand
sql.Connection = _cn
sql.CommandType = CommandType.Text = "update siswa set Nis ='" & Nisn & "',Nama_Siswa='" & Nama_Siswa & "',Jenis_Kelamin='" & Jenis_Kelamin & "',Kota_Lahir='" & Kota_Lahir & "',Tanggal_Lahir='" & Tanggal_Lahir & "',Agama='" & Agama & "',Berat_Badan='" & Berat_Badan & "',Tinggi_Badan='" & Tinggi_Badan & "'where Nis='" & Nis & "'"
ClassSiswa.cmd.ExecuteNonQuery()
sql.ExecuteNonQuery()
End Sub

这是SQL查询:

Create Database KPIRWAN
use KPIRWAN
Create Table siswa 
(
Nis int,
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)

问题是:在btnupdate编码中,当我试图以形式更新数据时,错误说从字符串"到类型"Integer"的转换对于无效

ClassSiswa.Nis = txt_nis.Text

从字符串"到类型"日期"的转换对于无效

ClassSiswa.Tanggal_Lahir = DTP_siswa.Text

如何修复此错误?

所有数据都是来自文本框的字符串。。。您需要根据需要强制转换,因此如果您需要一个整数,则需要使用CInt(txt_nis.Text)。。。您可能还需要CDate来表示日期等。除非您正在验证客户端,否则您可能也需要考虑验证值,例如。。

If IsNumeric(txt_nis.Text) Then
'value is numeric and can be assigned to ClassSiswa.Nis using CInt(txt_nis.Text)
Else
'value is not numeric handle error
End If

您可以使用IsDate检查日期

最新更新