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:
- close the connection if there is any open
- and then open a new connection for my database
- update the 'data1' and 'data2' where id = "the value of id.text texbox"
- check if "IMAGE" directory is exist, if not create
- 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"
- close the connection
- 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.