Link to home
Start Free TrialLog in
Avatar of patrickkenney
patrickkenney

asked on

Insert Image into SQL Database from VB.NET bound to id field in dataset.

Here is my question and I hope it is clear enough.

In an app I am building, I need to be able to insert and update images in a sql database table that are bound to the id field of the same table. In other words I want to be able to select which id a certain image will correspond with.

I would like to be able to bind the image field to a dataset I have created. I currently have several textboxes bound to a combobox bound to this dataset and would like the image field to be bound to the existing dataset in a similar way. Please provide code and examples. I am using Visual Studio Enterprise Architect 2003. Please let me know if you need more info. Thanks in advance!!!
Avatar of DotNetLover_Baan
DotNetLover_Baan

Avatar of patrickkenney

ASKER

I have seen that code and it works great, but what I need is the ability to select an image by a id. I have a database with employee information that I would like to insert images for. I would like to be able to select and employee and insert an image for him/her or update a new image. Can someone provide code for this? Thanks!!
Hi there,
change the code given in that example...
To select:
     Dim cmd As New SqlCommand("SELECT ImageField From MyTable WHERE EmpId=" & EmpId)
     cmd.Connection = con
     Dim da As New SqlDataAdapter(cmd)
     Dim ds As New DataSet()
     con.Open()
     da.Fill(ds)
     con.Close()
     Dim bits As Byte() = CType(ds.Tables(0).Rows(0).Item(0), Byte())
     Dim memorybits As New MemoryStream(bits)
     Dim bitmap As New Bitmap(memorybits)
     PictureBox1.Image = bitmap
   
To update:
     Dim Data() As Byte 'Say Data() has the binary data of the Image
     Dim cmd As New SqlCommand("UPDATE MyTable SET ImageField=" & Data &" WHERE EmpId=" & EmpId)
     cmd.Connection = con
     con.Open()
     cmd.ExecuteNonQuery
     con.Close()

-Baan
I receive an error when I try to use &Data& as a variable in the update statement. Help!
ASKER CERTIFIED SOLUTION
Avatar of DotNetLover_Baan
DotNetLover_Baan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what is the error you are getting ??
Well, I figured out what I was doing wrong, but now only the first page of the bitmap shows up when pulled out of the database? Any ideas? Thanks!
what do you mean by >>"first page of the bitmap "<< ??
Eh, just not my day. Was one of those ID10T errors. Thanks for you help!!!