I trying develop a program to save picture and data to my access database
the problem i facing is when user second time save or update data to the database. the saving data will not be change and it will be saved previous record below is my code thank a loti am trying use this code to refresh my form
Me.Hide()
Dim frm = New Form5
frm.Show()
Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
If PictureBox1.Image IsNot Nothing Then
Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
cmd.CommandText = "insert into Table1(Picture, F1, F2, F3)" &
"values (@picture, @F1, @F2, @F3)"
cmd.Parameters.AddWithValue("@picture", DataPic_Update)
Else
cmd.CommandText = "insert into Table1(F1, F2, F3)" &
"values (@F1, @F2, @F3)"
End If
cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)
TextBox_Value_Control()
cmd.ExecuteNonQuery()
RefreshData()
MessageBox.Show("Record Is Added")
End Sub
Private Sub Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Update.Click
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
cmd.Connection = cnn
If PictureBox1.Image IsNot Nothing Then
Dim DataPic_Update As Byte() = ImageToBytes(PictureBox1.Image, ImageFormat.Png)
cmd.CommandText = "UPDATE Table1 SET Picture = @picture, F1 = @F1, F2 = @F2, F3 = @F3 Where ID = " & ID.Text
cmd.Parameters.AddWithValue("@picture", DataPic_Update)
Else
cmd.CommandText = "UPDATE Table1 SET F1 = @F1, F2 = @F2, F3 = @F3 Where ID = " & ID.Text
End If
cmd.Parameters.AddWithValue("@F1", Me.TextBox1.Text)
cmd.Parameters.AddWithValue("@F2", Me.TextBox2.Text)
cmd.Parameters.AddWithValue("@F3", Me.TextBox3.Text)
cmd.ExecuteNonQuery()
RefreshData()
MsgBox("Update Data Successful", MsgBoxStyle.OkOnly, "Message")
End Sub
Private Function ImageToBytes(ByVal image As Image, ByVal format As ImageFormat) As Byte()
Using image_stream As MemoryStream = New MemoryStream()
image.Save(image_stream, format)
Return image_stream.ToArray()
End Using
End Function