By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

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

P: n/a
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


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)
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

Nov 20 '05 #2

P: n/a
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

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

P: n/a
On 3 Apr 2004 23:52:42 -0800, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #5

P: n/a
On 3 Apr 2004 23:52:42 -0800, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #6

P: n/a
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 <Us***********************@swspectrum.com> wrote in message news:<h0********************************@4ax.com>. ..
On 3 Apr 2004 23:52:42 -0800, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 20 '05 #7

P: n/a
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 <Us***********************@swspectrum.com> wrote in message news:<h0********************************@4ax.com>. ..
On 3 Apr 2004 23:52:42 -0800, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)

Nov 20 '05 #8

P: n/a
On 5 Apr 2004 16:50:45 -0700, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #9

P: n/a
On 5 Apr 2004 16:50:45 -0700, ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #10

P: n/a
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

P: n/a
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

P: n/a
On Thu, 08 Apr 2004 10:20:39 -0700, Abhishek Trivedi <ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #13

P: n/a
On Thu, 08 Apr 2004 10:20:39 -0700, Abhishek Trivedi <ab******@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 ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 20 '05 #14

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

This discussion thread is closed

Replies have been disabled for this discussion.