Click here to Skip to main content
15,881,839 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi everyone.
I am working on a project where user data is stored in sql db along with its image which is captured using a webcam.
I have finished the part where image is captured successfully and stored in picturebox, but the image from picturebox doesn't pass into the sql table, this part gives an error.

this is my code to insert picturebox image into sql database

VB
Dim cmd As New SqlCommand("INSERT INTO detail4 VALUES(@tag,@pic)", sql)
cmd.Parameters.AddWithValue("@name", Button1.Text)
Dim ms As New MemoryStream()
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@photo", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show("Name & Image has been saved", "Save",MessageBoxButtons.OK)



i get an error at
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)

is it because of rawformat... pls help...
Posted
Comments
Thomas Daniels 31-Mar-13 6:42am    
Which error do you get?
JPais 31-Mar-13 6:51am    
"Value cannot be null. Parameter name: encoder" at
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)

Hi,

Try:
VB
PictureBox1.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)

Hope this helps.
 
Share this answer
 
When i insert image into sqldatabase using store procedure it gives an error at "PictureBox1.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)" error is
>Object reference not set to an instance of an object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900