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

How do I dump Image data type in SQL Server

P: n/a
I have a column, of data type image, in a SQL table that I'm trying to
dump to a windows form control.

Does anyone have an example of this code and what control I would use
on the form?


Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies

P: n/a
Found it. Wouldn't you know it right after I posted my request;EN-US;321900

Nov 21 '05 #2

P: n/a

Ok from the example I was able to come up with the following code and I
get this error:

'System.ArgimentException' occured in system.drawing.dll
Invalid parameter used

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form1
Inherits System.Windows.Forms.Form
Dim cn As SqlConnection = New
#Region " Windows Form Designer generated code "
#End Region

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim cmd As SqlCommand = New SqlCommand("SELECT BlobCol FROM
MySQLTable", cn)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet

Dim c As Integer

da.Fill(ds, "dsTable")
c = ds.Tables("dsTable").Rows.Count

If c > 0 Then
Dim bytBLOBData() As Byte = ds.Tables("dsTable").Rows(c -
Dim stmBLOBData As New MemoryStream(bytBLOBData)
PBOX.Image = Image.FromStream(stmBLOBData)
End If
MsgBox("Image retrieved")

ds = Nothing
da = Nothing

cn = Nothing

End Sub
End Class

Nov 21 '05 #3

P: n/a

Have a look at this sample on our website, there is more.


I hope this helps,

Nov 21 '05 #4

P: n/a
Everthing works except the line where I assign the image to the
Picturebox.Image property. I take an invalid parm exception. See code
below. As a note, I was able to dump contents to a data file, but I
don't want to dump to a file and then turn around and post to the form.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim sqlcmd As New SqlCommand("SELECT ImageField FROM Mydb",
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim ms As New MemoryStream
Dim bytImage() As Byte
da.SelectCommand = sqlcmd

da.Fill(ds, "ByteTbl")

bytImage = CType(ds.Tables("ByteTbl").Rows(0)(0), Byte())

ms = New MemoryStream(bytImage)
Me.PBOX.Image = Image.FromStream(ms)

conn = Nothing
sqlcmd = Nothing
da = Nothing
ds = Nothing
ms = Nothing
bytImage = Nothing

End Sub

Nov 21 '05 #5

P: n/a

Are you sure that you have a table in your database Mydb which you name in
your dataset suddenly ByteTbl.

As well (not the reason that it is not working). Why do you do all those
crazy setting to nothing, it does nothing beside processing some code? All
those objects go automaticly out of scope in that procedure.

I hope this helps,

Nov 21 '05 #6

P: n/a
The table connection is working. I tested the connection / fill /
table (ByteTbl) content by dumping a couple of other columns.

The content isn't the problem, because I can dump the contents to a
flat file and interrogate it. By the way the contents of each data
cell is a Crystal Reports template. Every example I've seen about
dumping an image column to a win form Picture Box uses the above code.
So I'm going to try to store my own jpeg images to a table and use the
code above to pull it out again. If that works, then the Picture Box
control probably has a problem storing that kind of data.

Those crazy lines that set my objects to nothing is (1) by habit from
my C/C++/Java days where is was always good practice to do your our
house keeping; (2) do you really trust MS to do all your housekeeping?
Have you ever known a product to have a memory leak? Basically, the
code does hurt, for ME its just a good programming habit.

Nov 21 '05 #7

P: n/a

That good programming habit is as good as this

Sub A
dim a as integer
a = 0
End sub

However feel free to do this kind of code.
It is especially useful if you are paid per line.

And if it gives errors, than buy another compiler, if this is not done by
your compiler than it is really a bad product, in vendors who have these
compilers tell of course told that it is good practise.

Just my idea

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.