从字符串“SystemNullReferenceException:O”到“Integer”类型的转换无效

时间:2014-10-11 06:06:25

标签: mysql vb.net-2010

我是vb.net 2010的新手,我在将数据更新到mysql数据库时出现异常错误我不知道发生了什么,我无法解决问题。这是我的代码:

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

我想做的是:

  1. 如果有任何打开
  2. ,请关闭连接
  3. 然后为我的数据库打开一个新连接
  4. 更新' data1'和' data2'其中id =" id.text texbox"
  5. 的值
  6. 检查" IMAGE"目录存在,如果不是创建
  7. 检查picturebox2是否包含图像,如果包含则将图像保存为图像" IMAGE"目录名称为" id.text texbox"并且文件扩展名为" .png"
  8. 关闭连接
  9. 完成
  10. 但每次我调用此函数时我的数据都会更新,但它会给我一个异常错误,说明:

      

    无法更新数据。错误是转换表单字符串" SystemNullReferenceException:O"输入'整数'无效。

2 个答案:

答案 0 :(得分:0)

你是否更新了你的表,并且即使发生异常,这个函数也会将图像保存到指定的目录中...... 我觉得你给出的Mysql查询是问题所在。试试

sql = "update t_table1 set data1='" & data1.Text & "',data2='" & data2.Text & "',reg='" & reg & "' where id='" & id.text & "';"

试一试,因为我也没有测试过相同的...

答案 1 :(得分:0)

行。现在我修复了我自己的问题,为什么总是说从字符串转换中出现错误的原因是因为保存图像是错误的,并且当错误被try和catch函数捕获时,由于这一行,它总是说转换错误: MsgBox("The process failed: ", ex.ToString())我将其更改为MsgBox("The process failed: "& ex.message)并且没有错误弹出,但是图片没有保存,所以我做了一个更好的方法,首先我调用了这个函数:

Private sub updateData()
  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()

           dbcon.Close()
        Catch ex As Exception
            MsgBox("Unable to update data. Error is " & ex.Message)
            dbcon.Close()
            Exit Sub
        End Try

        updateImage()' call this to properly create directory and save the image
 End Sub

我创建了一个新功能' updateImage()'创建目录并保存新图像

Private sub updateImage()  
     Dim PicCopy As Image
     Dim path As String = "IMAGES/"
        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.Message)
                 End Try

           MsgBox("Records Successfully Update!", MsgBoxStyle.Information)
End Sub

然后它工作正常。没有错误。 谢谢!!即使我解决了自己的问题。