363,924 Members | 2570 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

fetch image data from access database to picture box in vb.net

Abhishek
P: n/a
Abhishek
can anybody tell how to fetch image from database(access) to the
visual basic picture box control. the code i have been using is as
below but it gives me invalid paramter error....

Dim connection As New OleDb.OleDbConnection()
Dim cmd As New OleDb.OleDbCommand()
cmd.CommandText = "select ID,Name,Description,Image from
MASTER where ID like '" & Trim(cmbID.Text) & "'"
cmd.Connection = connection
Dim myReader As OleDb.OleDbDataReader
cmd.CommandType = CommandType.Text
openOLEDBconnection(SQL_CONNECTION_STRING, connection)
myReader =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
While myReader.Read()
txtId.Text = myReader(0)
txtName.Text = myReader(1).ToString()
txtDescription.Text = myReader(2).ToString()
If Not IsDBNull(myReader(3)) Then
Try
Try
Dim a(myReader.GetBytes(3, 0, Nothing, 0,
Integer.MaxValue) - 1) As Byte
myReader.GetBytes(3, 0, a, 0, a.Length)
Dim b As New MemoryStream(a)
With pbImage
.Image = image.FromStream(b)
.SizeMode =
PictureBoxSizeMode.CenterImage
.BorderStyle = BorderStyle.Fixed3D
End With
Catch exp As Exception
MsgBox(exp.Message)
End Try
Catch exp As System.InvalidCastException
MsgBox(exp.Message)
End Try
End If
End While
myReader.Close()
Nov 20 '05 #1
Share this Question
Share on Google+
17 Replies


Cor
P: n/a
Cor
Hi Abhisek,

It has mostly to do with the enclosing from a image in an access file in an
object.

I once copied some code and have tried that to paste in your code, give it a
try?
(You have yourself to set it right in the try block again)

I did not test it, I just pasted it in, so please tell if it did work, than
I know that also?

Cor
\\\
Dim connection As New OleDb.OleDbConnection()
Dim cmd As New OleDb.OleDbCommand()
cmd.CommandText = "select ID,Name,Description,Image from
MASTER where ID like '" & Trim(cmbID.Text) & "'"
connection.open
Dim dr As OleDbDataReader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess )
dr.Read()
Dim FieldLen As Integer = dr.Item(1).ToString.Length
Dim a(FieldLen - 1) As Byte
Dim startIndex As Integer = 0
dim RetVal as long = dr.GetBytes(1, startIndex, a, 0, a.Length)
dr.Close()
connection.Close()
connection.dispose
Dim b As New MemoryStream(a)[color=blue]
> With pbImage
> .Image = image.FromStream(b)
> .SizeMode =
> PictureBoxSizeMode.CenterImage
> .BorderStyle = BorderStyle.Fixed3D
> End With
> Catch exp As Exception
> MsgBox(exp.Message)
> End Try
> Catch exp As System.InvalidCastException
> MsgBox(exp.Message)
> End Try
> End If
> End While[/color]


Nov 20 '05 #2

Abhishek
P: n/a
Abhishek
no it is not working that way also.
i have also tried the code given on microsoft site given below but it
also gives me the same error. am i missing somthing else. please help

Dim da As New OleDb.OleDbDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds, "MASTER")
Dim c As Integer = ds.Tables("MASTER").Rows.Count
If c > 0 Then
Dim bytBLOBData() As Byte = _
ds.Tables("MASTER").Rows(c - 1)("Image")
Dim stmBLOBData As New MemoryStream(bytBLOBData)
pbImage.Image = Image.FromStream(stmBLOBData)
end if
Nov 20 '05 #3

Cor
P: n/a
Cor
Hi Abhishek,

This code is for a standard blob not embeded in an ole object.
I do not know if you have that, however you can try to change this. It is
also better to always use Option Strict On, than this kind of problems (when
it is that) shows earlier

Ctype(ds.Tables("MASTER").Rows(c - 1)("Image"),Byte())

I hope this Helps?

Cor



Nov 20 '05 #4

Paul Clement
P: n/a
Paul Clement
On 3 Apr 2004 23:52:42 -0800, abhitriv@netscape.net (Abhishek) wrote:

¤ can anybody tell how to fetch image from database(access) to the
¤ visual basic picture box control. the code i have been using is as
¤ below but it gives me invalid paramter error....
¤
¤ Dim connection As New OleDb.OleDbConnection()
¤ Dim cmd As New OleDb.OleDbCommand()
¤ cmd.CommandText = "select ID,Name,Description,Image from
¤ MASTER where ID like '" & Trim(cmbID.Text) & "'"
¤ cmd.Connection = connection
¤ Dim myReader As OleDb.OleDbDataReader
¤ cmd.CommandType = CommandType.Text
¤ openOLEDBconnection(SQL_CONNECTION_STRING, connection)
¤ myReader =
¤ cmd.ExecuteReader(CommandBehavior.CloseConnection)
¤ While myReader.Read()
¤ txtId.Text = myReader(0)
¤ txtName.Text = myReader(1).ToString()
¤ txtDescription.Text = myReader(2).ToString()
¤ If Not IsDBNull(myReader(3)) Then
¤ Try
¤ Try
¤ Dim a(myReader.GetBytes(3, 0, Nothing, 0,
¤ Integer.MaxValue) - 1) As Byte
¤ myReader.GetBytes(3, 0, a, 0, a.Length)
¤ Dim b As New MemoryStream(a)
¤ With pbImage
¤ .Image = image.FromStream(b)
¤ .SizeMode =
¤ PictureBoxSizeMode.CenterImage
¤ .BorderStyle = BorderStyle.Fixed3D
¤ End With
¤ Catch exp As Exception
¤ MsgBox(exp.Message)
¤ End Try
¤ Catch exp As System.InvalidCastException
¤ MsgBox(exp.Message)
¤ End Try
¤ End If
¤ End While
¤ myReader.Close()

Couple of questions. What is the Access data type of the column that this image is stored in? How
was is originally stored?


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #5

Paul Clement
P: n/a
Paul Clement
On 3 Apr 2004 23:52:42 -0800, abhitriv@netscape.net (Abhishek) wrote:

¤ can anybody tell how to fetch image from database(access) to the
¤ visual basic picture box control. the code i have been using is as
¤ below but it gives me invalid paramter error....
¤
¤ Dim connection As New OleDb.OleDbConnection()
¤ Dim cmd As New OleDb.OleDbCommand()
¤ cmd.CommandText = "select ID,Name,Description,Image from
¤ MASTER where ID like '" & Trim(cmbID.Text) & "'"
¤ cmd.Connection = connection
¤ Dim myReader As OleDb.OleDbDataReader
¤ cmd.CommandType = CommandType.Text
¤ openOLEDBconnection(SQL_CONNECTION_STRING, connection)
¤ myReader =
¤ cmd.ExecuteReader(CommandBehavior.CloseConnection)
¤ While myReader.Read()
¤ txtId.Text = myReader(0)
¤ txtName.Text = myReader(1).ToString()
¤ txtDescription.Text = myReader(2).ToString()
¤ If Not IsDBNull(myReader(3)) Then
¤ Try
¤ Try
¤ Dim a(myReader.GetBytes(3, 0, Nothing, 0,
¤ Integer.MaxValue) - 1) As Byte
¤ myReader.GetBytes(3, 0, a, 0, a.Length)
¤ Dim b As New MemoryStream(a)
¤ With pbImage
¤ .Image = image.FromStream(b)
¤ .SizeMode =
¤ PictureBoxSizeMode.CenterImage
¤ .BorderStyle = BorderStyle.Fixed3D
¤ End With
¤ Catch exp As Exception
¤ MsgBox(exp.Message)
¤ End Try
¤ Catch exp As System.InvalidCastException
¤ MsgBox(exp.Message)
¤ End Try
¤ End If
¤ End While
¤ myReader.Close()

Couple of questions. What is the Access data type of the column that this image is stored in? How
was is originally stored?


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #6

Abhishek
P: n/a
Abhishek
The Image is Stored in access database in OLE Type of Column. it was
stored using the access form wizard.

can the image data retrieved from access be in wrong format?

Paul Clement <UseAdddressAtEndofMessage@swspectrum.com> wrote in message news:<h07370dvmec7u4ug9ons2s0gbij4psll28@4ax.com>. ..[color=blue]
> On 3 Apr 2004 23:52:42 -0800, abhitriv@netscape.net (Abhishek) wrote:
>
> ¤ can anybody tell how to fetch image from database(access) to the
> ¤ visual basic picture box control. the code i have been using is as
> ¤ below but it gives me invalid paramter error....
> ¤
> ¤ Dim connection As New OleDb.OleDbConnection()
> ¤ Dim cmd As New OleDb.OleDbCommand()
> ¤ cmd.CommandText = "select ID,Name,Description,Image from
> ¤ MASTER where ID like '" & Trim(cmbID.Text) & "'"
> ¤ cmd.Connection = connection
> ¤ Dim myReader As OleDb.OleDbDataReader
> ¤ cmd.CommandType = CommandType.Text
> ¤ openOLEDBconnection(SQL_CONNECTION_STRING, connection)
> ¤ myReader =
> ¤ cmd.ExecuteReader(CommandBehavior.CloseConnection)
> ¤ While myReader.Read()
> ¤ txtId.Text = myReader(0)
> ¤ txtName.Text = myReader(1).ToString()
> ¤ txtDescription.Text = myReader(2).ToString()
> ¤ If Not IsDBNull(myReader(3)) Then
> ¤ Try
> ¤ Try
> ¤ Dim a(myReader.GetBytes(3, 0, Nothing, 0,
> ¤ Integer.MaxValue) - 1) As Byte
> ¤ myReader.GetBytes(3, 0, a, 0, a.Length)
> ¤ Dim b As New MemoryStream(a)
> ¤ With pbImage
> ¤ .Image = image.FromStream(b)
> ¤ .SizeMode =
> ¤ PictureBoxSizeMode.CenterImage
> ¤ .BorderStyle = BorderStyle.Fixed3D
> ¤ End With
> ¤ Catch exp As Exception
> ¤ MsgBox(exp.Message)
> ¤ End Try
> ¤ Catch exp As System.InvalidCastException
> ¤ MsgBox(exp.Message)
> ¤ End Try
> ¤ End If
> ¤ End While
> ¤ myReader.Close()
>
> Couple of questions. What is the Access data type of the column that this image is stored in? How
> was is originally stored?
>
>
> Paul ~~~ pclement@ameritech.net
> Microsoft MVP (Visual Basic)[/color]
Nov 20 '05 #7

Abhishek
P: n/a
Abhishek
The Image is Stored in access database in OLE Type of Column. it was
stored using the access form wizard.

can the image data retrieved from access be in wrong format?

Paul Clement <UseAdddressAtEndofMessage@swspectrum.com> wrote in message news:<h07370dvmec7u4ug9ons2s0gbij4psll28@4ax.com>. ..[color=blue]
> On 3 Apr 2004 23:52:42 -0800, abhitriv@netscape.net (Abhishek) wrote:
>
> ¤ can anybody tell how to fetch image from database(access) to the
> ¤ visual basic picture box control. the code i have been using is as
> ¤ below but it gives me invalid paramter error....
> ¤
> ¤ Dim connection As New OleDb.OleDbConnection()
> ¤ Dim cmd As New OleDb.OleDbCommand()
> ¤ cmd.CommandText = "select ID,Name,Description,Image from
> ¤ MASTER where ID like '" & Trim(cmbID.Text) & "'"
> ¤ cmd.Connection = connection
> ¤ Dim myReader As OleDb.OleDbDataReader
> ¤ cmd.CommandType = CommandType.Text
> ¤ openOLEDBconnection(SQL_CONNECTION_STRING, connection)
> ¤ myReader =
> ¤ cmd.ExecuteReader(CommandBehavior.CloseConnection)
> ¤ While myReader.Read()
> ¤ txtId.Text = myReader(0)
> ¤ txtName.Text = myReader(1).ToString()
> ¤ txtDescription.Text = myReader(2).ToString()
> ¤ If Not IsDBNull(myReader(3)) Then
> ¤ Try
> ¤ Try
> ¤ Dim a(myReader.GetBytes(3, 0, Nothing, 0,
> ¤ Integer.MaxValue) - 1) As Byte
> ¤ myReader.GetBytes(3, 0, a, 0, a.Length)
> ¤ Dim b As New MemoryStream(a)
> ¤ With pbImage
> ¤ .Image = image.FromStream(b)
> ¤ .SizeMode =
> ¤ PictureBoxSizeMode.CenterImage
> ¤ .BorderStyle = BorderStyle.Fixed3D
> ¤ End With
> ¤ Catch exp As Exception
> ¤ MsgBox(exp.Message)
> ¤ End Try
> ¤ Catch exp As System.InvalidCastException
> ¤ MsgBox(exp.Message)
> ¤ End Try
> ¤ End If
> ¤ End While
> ¤ myReader.Close()
>
> Couple of questions. What is the Access data type of the column that this image is stored in? How
> was is originally stored?
>
>
> Paul ~~~ pclement@ameritech.net
> Microsoft MVP (Visual Basic)[/color]
Nov 20 '05 #8

Paul Clement
P: n/a
Paul Clement
On 5 Apr 2004 16:50:45 -0700, abhitriv@netscape.net (Abhishek) wrote:

¤ The Image is Stored in access database in OLE Type of Column. it was
¤ stored using the access form wizard.
¤
¤ can the image data retrieved from access be in wrong format?

I'm trying to determine whether the image was stored as an OLE object, which would mean that it
contains OLE headers. This makes the retrieval somewhat more difficult.

Do you know whether it was stored as an OLE object or was it written as a binary stream from a file?
Also, what type of image is this? BMP? JPG?


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #9

Paul Clement
P: n/a
Paul Clement
On 5 Apr 2004 16:50:45 -0700, abhitriv@netscape.net (Abhishek) wrote:

¤ The Image is Stored in access database in OLE Type of Column. it was
¤ stored using the access form wizard.
¤
¤ can the image data retrieved from access be in wrong format?

I'm trying to determine whether the image was stored as an OLE object, which would mean that it
contains OLE headers. This makes the retrieval somewhat more difficult.

Do you know whether it was stored as an OLE object or was it written as a binary stream from a file?
Also, what type of image is this? BMP? JPG?


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #10

Abhishek Trivedi
P: n/a
Abhishek Trivedi
I think that the file is stored as ole object. because when i stored the
retrived data in a file it showed me the extra header.
so how is it possible to retrive the ole image from access in dot net



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #11

Abhishek Trivedi
P: n/a
Abhishek Trivedi
I think that the file is stored as ole object. because when i stored the
retrived data in a file it showed me the extra header.
so how is it possible to retrive the ole image from access in dot net



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #12

Paul Clement
P: n/a
Paul Clement
On Thu, 08 Apr 2004 10:20:39 -0700, Abhishek Trivedi <abhitriv@netscape.net> wrote:

¤ I think that the file is stored as ole object. because when i stored the
¤ retrived data in a file it showed me the extra header.
¤ so how is it possible to retrive the ole image from access in dot net
¤

Well each image type will likely have a different OLE header. Does each row have the same image type
and if so what is the image type? (e.g. BMP, JPG, etc.)


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #13

Paul Clement
P: n/a
Paul Clement
On Thu, 08 Apr 2004 10:20:39 -0700, Abhishek Trivedi <abhitriv@netscape.net> wrote:

¤ I think that the file is stored as ole object. because when i stored the
¤ retrived data in a file it showed me the extra header.
¤ so how is it possible to retrive the ole image from access in dot net
¤

Well each image type will likely have a different OLE header. Does each row have the same image type
and if so what is the image type? (e.g. BMP, JPG, etc.)


Paul ~~~ pclement@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #14

Abhishek Trivedi
P: n/a
Abhishek Trivedi
yes all the images are of the same type i.e. *.bmp



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #15

Abhishek Trivedi
P: n/a
Abhishek Trivedi
yes all the images are of the same type i.e. *.bmp



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #16

Abhishek
P: n/a
Abhishek
can anybody solve my problem please. i have to show images stored in
access database(ole column type) in visual basic .net picturebox
control.thanx
Nov 20 '05 #17

Abhishek
P: n/a
Abhishek
can anybody solve my problem please. i have to show images stored in
access database(ole column type) in visual basic .net picturebox
control.thanx
Nov 20 '05 #18

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET