我正在为visual studio 2012中的更新功能编码,但是,有一个“invalidcastexception未处理”错误。
这是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编码中,当我试图更新数据时,表示错误说 “从字符串”“转换为”整数“类型无效”for:
ClassSiswa.Nis = txt_nis.Text
或
从字符串“”到“日期”类型的转换对以下内容无效:
ClassSiswa.Tanggal_Lahir = DTP_siswa.Text
如何修复此错误?
答案 0 :(得分:0)
您的所有数据都是来自文本框的字符串......您需要根据需要强制它们,因此如果您需要整数,则需要使用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