This is the Code:
When I select the row in datagridview, and click on delete button it doesn't delete.
Am I wrong in the code ?
Private Sub Save_Image()
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim sSQL As String = String.Empty
Dim arrImage() As Byte
Dim myMs As New IO.MemoryStream
Dim bSaveImage As Boolean = False
Dim strImg As String = String.Empty
Try
If Not IsNothing(Me.myimage.Image) Then
Me.myimage.Image.Save(myMs, Me.myimage.Image.RawFormat)
arrImage = myMs.GetBuffer
bSaveImage = True
Else
arrImage = Nothing
bSaveImage = False
End If
If bSaveImage = True Then
If Microsoft.VisualBasic.Right(Application.StartupPat h, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "SampleDB.accdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\SampleDB.accdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
If Me.txtDescription.Tag = 0 Then
cmd.CommandText = "INSERT INTO tbl_image (image_description, [image] ) VALUES(@img_desc, @img)"
Else
cmd.CommandText = "UPDATE tbl_image set image_description = @img_desc, [image] = @img where id=@id"
End If
cmd.Parameters.Add("@img_desc", OleDbType.VarChar).Value = Me.txtDescription.Text
cmd.Parameters.Add("@img", OleDb.OleDbType.Binary).Value = arrImage
cmd.Parameters.Add("@id", OleDbType.BigInt).Value = Me.txtDescription.Tag
cmd.ExecuteNonQuery()
MsgBox("Image has been save.")
Else
MsgBox("Please add an image")
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
Private Sub Load_Image(ByVal iIMageID As Long)
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim dr As OleDbDataReader
Dim arrImage() As Byte
Dim myMS As New IO.MemoryStream
Try
If Microsoft.VisualBasic.Right(Application.StartupPat h, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "SampleDB.accdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\SampleDB.accdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, image_description as description, [image] from tbl_image where id= " & iIMageID
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read
Me.txtDescription.Tag = dr("id")
Me.txtDescription.Text = dr("description")
arrImage = dr("image")
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
'
Me.myimage.Image = System.Drawing.Image.FromStream(myMS)
End While
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
'PROBLEM EXISTS HERE
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim SqlQuery As String = "DELETE FROM tbl_image WHERE ID = " & Me.DataGridView1.SelectedRows(0).Cells(0).Value.To String & ";"
Dim SqlCommand As New OleDbCommand
Dim conn As New OleDbConnection
With SqlCommand
.CommandText = SqlQuery
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("Your record is successfully deleted!")
End Sub
Private Sub Load_GridView()
Dim conn As New OleDbConnection
Dim sConnString As String
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Try
If Microsoft.VisualBasic.Right(Application.StartupPat h, 1) = "\" Then
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "SampleDB.accdb;Persist Security Info=False;"
Else
sConnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\SampleDB.accdb;Persist Security Info=False;"
End If
conn = New OleDbConnection(sConnString)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "select id, image_description as description from tbl_image"
da.SelectCommand = cmd
da.Fill(dt)
Me.DataGridView1.DataSource = dt
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
Private Sub DataGridView1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
Load_Image(Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value)
End Sub