我是vb.net 2010中的新手,我在更新数据到mysql数据库时出现异常错误我不知道发生了什么,我无法解决问题。这里是我的代码:从字符串“SystemNullReferenceException:O”到字符串“Integer”的转换无效
Private sub updateData()
Dim path As String = "IMAGES/"
Dim reg As String = 1
dbcon.Close() ' This will close any open connection
con = "server=localhost; uid=root;pwd=;database=myDatabase;"
Try
dbcon.ConnectionString = con
dbcon.Open()
sql = "update t_table1 set data1='" & data1.Text & "',data2='" & data2.Text & "',"',reg='" & reg & "' where id='" & id.text & "';"
dbcom = New MySqlCommand(sql, dbcon)
dbcom.ExecuteNonQuery()
Try
If Not Directory.Exists(path) Then
'This will create a directory "IMAGE"
Dim di As DirectoryInfo = Directory.CreateDirectory(path)
End If
If Not PictureBox1.Image Is Nothing Then
'If the picturebox contain an image then it will save into "IMAGE"directory
PicCopy.Save(String.Concat(path, id.Text, ".png"))
Else
End If
Catch ex As Exception
MsgBox("The process failed: ", ex.ToString())
End Try
MsgBox("Records Successfully Update!", MsgBoxStyle.Information)
dbcon.Close()
Catch ex As Exception
MsgBox("Unable to update data. Error is " & ex.Message)
dbcon.Close()
Exit Sub
End Try
End Sub
我所试图做的是:
- 关闭连接,如果有任何打开的
- ,然后打开我的数据库
- 更新一个新的连接'data1'和'data2'其中id =“id.text texbox的值”
- 检查“IMAGE”目录是否存在,如果没有创建
- 检查if picturebox2包含一个图像,如果包含然后保存图像ITO“形象”目录中的“id.text texbox”的名字,并以“png格式”
- 关闭进行连接
- 文件扩展名
但我每次调用这个函数,我的数据时间更新,但其给我一个异常错误说:
无法更新数据。错误是转换形式字符串“SystemNullReferenceException:O”键入“整数”无效。
您的SQL查询**过时**。阅读此:[如何创建参数化的SQL查询?为什么我应该?](http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i)和这个:[给我参数化的SQL,或者给我死亡](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/)。 – 2014-10-11 07:46:19