B
Bajtitou
Guest
I want To Insert Image into Access Database,
But I am getting " A generic error occurred in GDI+ ".
First I'm Displaying Image From database To DataGridView, then From DataGridView To PictureBox.
I want To Insert This Image from PictureBox To DataBase.
This the code I an Using For This:
Code to display image from Dgv1 to PictureBox:
Private Sub Dgv1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Dgv1.CellContentClick
Dim f1 as New Form3
Try
Dim dr As Integer = Dgv1.CurrentCell.RowIndex + 0
If dr > -1 Then
f1.CboRaces.Text = CStr(Dgv1.Rows(dr).Cells(2).Value)
f1.txtCage.Text = CInt(Dgv1.Rows(dr).Cells(3).Value)
If IsDBNull(Dgv1.Rows(dr).Cells("Photo").Value) = True Then
f1.Picture.Image = My.Resources.GiGi
Else
Dim data As Byte() = Dgv1.Rows(dr).Cells("Photo").Value
Using ms As New MemoryStream(data)
f1.Picture.Image = Image.FromStream(ms)
End Using
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Code To Insert Image To DataBase:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Using cn As New OleDb.OleDbConnection With
{
.ConnectionString = Builder.ConnectionString
}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cn.Open()
cmd.CommandText = "INSERT INTO Pictures(CoupleID,Photo) VALUES(@d1,@d2)"
cmd.Parameters.AddWithValue("@d1", txtCode.Text)
Dim ms As New MemoryStream()
Picture.Image.Save(ms, Picture.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New OleDbParameter("@d2", OleDbType.Binary)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show(" Image has been saved", "Save", MessageBoxButtons.OK)
cmd.Parameters.Clear()
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Code To Display Image From Database To DataGridview:
Module LoadPontes
Public Function LoadDatas() As DataTable
Dim dt As New DataTable
Using cn As New OleDb.OleDbConnection With
{
.ConnectionString = Builder.ConnectionString
}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cn.Open()
Dim selectStatement As String =
<SQL>
SELECT TestingTable1.CoupleID
, TestingTable1.Pontes
, TestingTable1.Race
,TestingTable1.Cage
,Photo
FROM TestingTable1 ,Pictures where Pontes like 'Ponte1' and TestingTable1.CoupleID= Pictures.CoupleID Order By Pontes
</SQL>.Value
cmd.CommandText = selectStatement
dt.Load(cmd.ExecuteReader())
Return dt
End Using
End Using
End Function
End Module
'Load Datas into Dgv1
Private Sub btnPonte2_Click(sender As Object, e As EventArgs) Handles btnPonte2.Click
Dgv1.DataSource = LoadDatas()
End Sub
This Is all What I am Using.
So thank you Very Much For your help.
Best Regards.
Continue reading...
But I am getting " A generic error occurred in GDI+ ".
First I'm Displaying Image From database To DataGridView, then From DataGridView To PictureBox.
I want To Insert This Image from PictureBox To DataBase.
This the code I an Using For This:
Code to display image from Dgv1 to PictureBox:
Private Sub Dgv1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Dgv1.CellContentClick
Dim f1 as New Form3
Try
Dim dr As Integer = Dgv1.CurrentCell.RowIndex + 0
If dr > -1 Then
f1.CboRaces.Text = CStr(Dgv1.Rows(dr).Cells(2).Value)
f1.txtCage.Text = CInt(Dgv1.Rows(dr).Cells(3).Value)
If IsDBNull(Dgv1.Rows(dr).Cells("Photo").Value) = True Then
f1.Picture.Image = My.Resources.GiGi
Else
Dim data As Byte() = Dgv1.Rows(dr).Cells("Photo").Value
Using ms As New MemoryStream(data)
f1.Picture.Image = Image.FromStream(ms)
End Using
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Code To Insert Image To DataBase:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Try
Using cn As New OleDb.OleDbConnection With
{
.ConnectionString = Builder.ConnectionString
}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cn.Open()
cmd.CommandText = "INSERT INTO Pictures(CoupleID,Photo) VALUES(@d1,@d2)"
cmd.Parameters.AddWithValue("@d1", txtCode.Text)
Dim ms As New MemoryStream()
Picture.Image.Save(ms, Picture.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New OleDbParameter("@d2", OleDbType.Binary)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show(" Image has been saved", "Save", MessageBoxButtons.OK)
cmd.Parameters.Clear()
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Code To Display Image From Database To DataGridview:
Module LoadPontes
Public Function LoadDatas() As DataTable
Dim dt As New DataTable
Using cn As New OleDb.OleDbConnection With
{
.ConnectionString = Builder.ConnectionString
}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cn.Open()
Dim selectStatement As String =
<SQL>
SELECT TestingTable1.CoupleID
, TestingTable1.Pontes
, TestingTable1.Race
,TestingTable1.Cage
,Photo
FROM TestingTable1 ,Pictures where Pontes like 'Ponte1' and TestingTable1.CoupleID= Pictures.CoupleID Order By Pontes
</SQL>.Value
cmd.CommandText = selectStatement
dt.Load(cmd.ExecuteReader())
Return dt
End Using
End Using
End Function
End Module
'Load Datas into Dgv1
Private Sub btnPonte2_Click(sender As Object, e As EventArgs) Handles btnPonte2.Click
Dgv1.DataSource = LoadDatas()
End Sub
This Is all What I am Using.
So thank you Very Much For your help.
Best Regards.
Continue reading...