Possible to save Picture into database using DataAdapter?

  • Thread starter Thread starter Mika M
  • Start date Start date
M

Mika M

Hi!

I'd like to know is it possible to save picture into MSDE 2000 (SP4)
database tables 'Image'-field using DataAdapter ?

I've made it to work like links below shows:

http://support.microsoft.com/default.aspx?scid=kb;en-us;317670
http://support.microsoft.com/default.aspx?scid=kb;en-us;321900

....and it works fine, but because I retrieve image of the same DB table
of the MSDE than some text fields, so I'd like to use same DataAdapter
for those all if it is possible, and not to save image different way
than other fields of the same table.
 
Hi Mika,

Certainly, why not?
Just add a parameter of type SqlDbType.Image to your adapter's command(s)
and create proper sql statement(s).
The column in DataTable should be of byte[] type.
 
Miha said:
Hi Mika,

Certainly, why not?
Just add a parameter of type SqlDbType.Image to your adapter's command(s)
and create proper sql statement(s).
The column in DataTable should be of byte[] type.

Well... Maybe I wrote my question quite difficult way to understand.
It's difficult to translate my finnish thoughts into english :)

Anyway I'm trying to convert that style like this way...


Dim bndImage As Binding = New Binding("Image", myDataTable, "Image")
picImage.DataBindings.Add(bndImage)
AddHandler bndImage.Format, AddressOf BndFormatImage


Private Sub BndFormatImage(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If e.DesiredType Is GetType(Image) Then
If IsDBNull(e.Value) Then
e.Value = Nothing
Else
Dim bytes() As Byte = CType(e.Value, Byte())
Dim str As MemoryStream = New MemoryStream(bytes)
Dim img As Image = Image.FromStream(str)
str.Close()

'// Re-assign formatted data
e.Value = img
End If
End If
End Sub


At once the second line of code causes error message:

"Invalid cast from System.DBNull to System.Drawing.Image."

....so I can't get it to work especially with "no picture" case. What's
wrong with it.
 
Hi Mika,

Instead of passing e.Value = null try passing e.Value = new Bitmap(something
here) instead (pass blank image)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Mika M said:
Miha said:
Hi Mika,

Certainly, why not?
Just add a parameter of type SqlDbType.Image to your adapter's command(s)
and create proper sql statement(s).
The column in DataTable should be of byte[] type.

Well... Maybe I wrote my question quite difficult way to understand. It's
difficult to translate my finnish thoughts into english :)

Anyway I'm trying to convert that style like this way...


Dim bndImage As Binding = New Binding("Image", myDataTable, "Image")
picImage.DataBindings.Add(bndImage)
AddHandler bndImage.Format, AddressOf BndFormatImage


Private Sub BndFormatImage(ByVal sender As Object, ByVal e As
ConvertEventArgs)
If e.DesiredType Is GetType(Image) Then
If IsDBNull(e.Value) Then
e.Value = Nothing
Else
Dim bytes() As Byte = CType(e.Value, Byte())
Dim str As MemoryStream = New MemoryStream(bytes)
Dim img As Image = Image.FromStream(str)
str.Close()

'// Re-assign formatted data
e.Value = img
End If
End If
End Sub


At once the second line of code causes error message:

"Invalid cast from System.DBNull to System.Drawing.Image."

...so I can't get it to work especially with "no picture" case. What's
wrong with it.
 
Mika,

See this sample, however there are more samples with images on our website.

I hope this helps,

Cor
 
Back
Top