-1

I am new in vb.net 2010, I am stock in an exception error while updating data into mysql database I don't know what's going on, I can't fixed the problem. Here is my code:

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

What I am trying to do is:

  1. close the connection if there is any open
  2. and then open a new connection for my database
  3. update the 'data1' and 'data2' where id = "the value of id.text texbox"
  4. check if "IMAGE" directory is exist, if not create
  5. check if picturebox2 contain an image, if contain then save the image ito "IMAGE" directory with the name of "id.text texbox" and with the file extension of ".png"
  6. close the connection
  7. done

but every time I call this function my data was update but its give me an exception error saying that:

Unable to update data. Error is Conversion form string "SystemNullReferenceException: O" to type 'Integer' is not valid.

Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
Polar
  • 3,327
  • 4
  • 42
  • 77
  • Your sql query is **obsolete**. Read this: [How do I create a parameterized SQL query? Why Should I?](http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i) and this: [Give me parameterized SQL, or give me death](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). – Bjørn-Roger Kringsjå Oct 11 '14 at 07:46

2 Answers2

0

did u get your table updated and also will this function save image to the specified directory even if exception occurs...?? I feel like the Mysql Query you have given is the problem here.try

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

just give a try as i also haven't tested the same...

a4akhil
  • 1
  • 2
  • thanks.,there's no any problem in query, i already try it in mysql command line and in vb.net, it works fine, i think my problem is in image saving, but i don't know how to fixed. – Polar Oct 11 '14 at 07:43
  • i try to remove this line in my code `PicCopy.Save(String.Concat(path, id.Text, ".png"))` and no error exception occurred, so maybe this line is my problem, this line is for saving the image into "IMAGE/" directory. – Polar Oct 11 '14 at 07:45
0

OK. Now i fixed my own problem, the reason why there is always saying error in conversion from string etc. is because saving image is error, and when error is catching by try and catch function it is always saying error in conversion because of this line: MsgBox("The process failed: ", ex.ToString()) i change it into MsgBox("The process failed: "& ex.message) and no error popup, but the image is not saving, so i made a better way, first i cal the function:

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

i made a new function 'updateImage()' to create directory and save the new image

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

And Then it works fine. And no error. Thanks!! even i solve my own problem.

Polar
  • 3,327
  • 4
  • 42
  • 77