在Visual Basic 2012 OOP方法中更新功能 [英] update function in visual basic 2012 OOP method
问题描述
我正在为visual studio 2012中的更新功能编码,但是,有一个invalidcastexception未处理错误。
这是编码Windows窗体中的更新功能:
i'm working on a coding for update function in visual studio 2012 but, there is an "invalidcastexception was unhandled" error.
this is the coding for update function in windows form:
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
这是该功能的类:
and this is the class for the function:
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查询:
and this is the SQL query:
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'类型无效:
the function i want is after i searched the data with search button, the data can be changed or updated with update button.
the problem is:
in the btnupdate coding, when i tried to update data in form the error said
Conversion from string "" to type 'Integer' is not valid for:
ClassSiswa.Nis = txt_nis.Text
或
从字符串到日期类型的转换不是适用于:
or
Conversion from string "" to type 'date' is not valid for:
ClassSiswa.Tanggal_Lahir = DTP_siswa.Text
如何修复此错误?
how do fix this error?
推荐答案
您无法直接从文本字符串设置整数或日期值。它们首先需要通过 Parse之一转换为正确的类型 [ ^ $或者TryParse方法。
你也不应该为你的SQL命令使用字符串代替,使用正确的参数化查询。
You cannot set an integer or a date value directly from a text string. They first need to be converted to the correct type, by one of the Parse[^] or TryParse methods.
You should also not use string concetenation for your SQL commands, use proper parameterised queries.
解决方案2解释了你做什么而不是演员表,让我们来看看你通过字符串连接获得的查询。
查询由串联组成从UI获取的字符串。不仅重复的字符串连接是低效的(因为字符串是不可变的;我是否必须解释为什么它会使重复连接变坏?),但是有更重要的问题:它打开了通向良好的大门已知的漏洞称为 SQL注入。
这是它的工作原理: http://xkcd.com/327 。
怎么办?只需阅读有关此问题和主要补救措施:参数化语句: http://en.wikipedia.org/ wiki / SQL_injection 。
使用ADO.NET,使用:http://msdn.microsoft.com/en-us/library/ff648339.aspx 。
请参阅我过去的答案有更多细节:
在com.ExecuteNonQuery中更新EROR( ); ,
嗨名字没有显示在名称中?。
-SA
Solution 2 explains you what do to instead of the cast, and let's look at your query obtained by string concatenation.
The query is composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.
This is how it works: http://xkcd.com/327.
What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.
With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.
Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.
—SA
这篇关于在Visual Basic 2012 OOP方法中更新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!