Solved

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

Posted on 2004-08-17
9
295 Views
Last Modified: 2008-03-10
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!!!
0
Comment
Question by:patrickkenney
  • 5
  • 4
9 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11825846
0
 

Author Comment

by:patrickkenney
ID: 11826975
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!!
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11827383
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
0
 

Author Comment

by:patrickkenney
ID: 11830983
I receive an error when I try to use &Data& as a variable in the update statement. Help!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 11832543
First read the image file into Data()...

            Dim fs As New FileStream(YourFile.jpg, FileMode.Open)
            Dim Data() As Byte = New [Byte](fs.Length) {}
            fs.Read(Data, 0, fs.Length)

Then update, with SqlCommand.
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11832547
what is the error you are getting ??
0
 

Author Comment

by:patrickkenney
ID: 11835369
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!
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 11835451
what do you mean by >>"first page of the bitmap "<< ??
0
 

Author Comment

by:patrickkenney
ID: 11835508
Eh, just not my day. Was one of those ID10T errors. Thanks for you help!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Encryption Decryption in Oracle 12 107
Loop through Multiple Processes Async 2 28
More on Time zones in vb 2010 12 37
Make all values in arrayist to titlecase+vb.net 23 27
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now