2014-10-11 84 views
-1

我是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 

我所试图做的是:

  1. 关闭连接,如果有任何打开的
  2. ,然后打开我的数据库
  3. 更新一个新的连接'data1'和'data2'其中id =“id.text texbox的值”
  4. 检查“IMAGE”目录是否存在,如果没有创建
  5. 检查if picturebox2包含一个图像,如果包含然后保存图像ITO“形象”目录中的“id.text texbox”的名字,并以“png格式”
  6. 关闭进行连接
  7. 文件扩展名

但我每次调用这个函数,我的数据时间更新,但其给我一个异常错误说:

无法更新数据。错误是转换形式字符串“SystemNullReferenceException:O”键入“整数”无效。

+0

您的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

回答

0

确定。现在我解决了自己的问题,之所以总是说从字符串等转换错误是因为保存图像是错误的,并且当通过try和catch函数捕获错误时,它总是在说因为这一行而导致转换错误: MsgBox("The process failed: ", ex.ToString())我把它变成MsgBox("The process failed: "& ex.message)并没有错误弹出,但图像没有保存,所以我做了一个更好的办法,首先我CAL功能:

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 

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

0

是否让你的表更新,并且这个函数会保存图像到指定的目录,即使发生异常...... ?? 我觉得你已经给出了MySQL查询的问题here.try

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

只是给一个尝试,因为我还没有测试一样......

+0

谢谢。在查询中没有任何问题,我已经在mysql命令行和vb.net中尝试了它,它工作正常,我认为我的问题是在图像保存,但我不知道如何修复。 – Polar 2014-10-11 07:43:03

+0

我尝试删除我的代码中的这一行 'PicCopy.Save(String.Concat(path,id.Text,“)。png“))' 并且没有发生错误异常,所以也许这行是我的问题, 这一行是为了将图像保存到”IMAGE /“目录。 – Polar 2014-10-11 07:45:22

相关问题