Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
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
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
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 31-Mar-13 1:36am
ProgramFOX at 31-Mar-13 6:42am
Which error do you get?
JPais at 31-Mar-13 6:51am
"Value cannot be null. Parameter name: encoder" at
PictureBox1.BackgroundImage.Save(ms, PictureBox1.BackgroundImage.RawFormat)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

PictureBox1.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Hope this helps.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Thank u so much, your code works.
But there is a problem. I retrieved the image in datagrid view and there it is viewed as error image (red cross)
What may be the mistake i am doing??
ProgramFOX at 31-Mar-13 8:22am
What's the code to retrieve the image?
JPais at 10-Apr-13 8:23am
extremely sorry for the late reply..
I am not retrieving the image in picturebox, instead i am just displaying it in datagrid view.
I am using the following code in textbox_change
query = "select * from detail4 where tag like '" & TextBox1.Text & "%'"
DataGridView1.DataSource = Nothing
Dim da As SqlDataAdapter = New SqlDataAdapter("", sql)
da.SelectCommand = New SqlCommand(query, sql)
Dim ds As New DataSet
If ds.Tables.Count > 0 Then
DataGridView1.DataSource = ds.Tables(0).DefaultView
End If
Catch ex As Exception
End Try
again sorry for da late reply..
ProgramFOX at 10-Apr-13 8:27am
instead i am just displaying it in datagrid view
Then I've no idea, I'm sorry.
JPais at 10-Apr-13 8:53am
sorry..dint get u??
i have made a function which contains the entire try catch block..
Which i call just after the query.
It only displays the image in datagrid view.
I too don't know how to get it in picture box, I am working on it.
ProgramFOX at 10-Apr-13 8:55am
sorry..dint get u??
I just have no idea about how to load image in datagridview.
JPais at 10-Apr-13 9:00am
thanks anyways..
can u pls help me with the query i posted below..
Do u have any idea why i am getting this exception and error
RedDK at 31-Mar-13 12:53pm
This is what happens when you start a conversation in another Solution box. One: op gets a thumbdown for question and .. Two: answerer gets a thumbdown for engaging him in his "answer" post. Badness.
JPais at 10-Apr-13 8:24am
Sorry Sir.
I am new to code project.
JPais at 10-Apr-13 8:47am
I have a button on whose click I have the following tasks carried out
1. Captures image from webcam
2. stores it in database
on first click of button, this process carries out successfully but on the next click i get this exception
A variable name '@id' has already been declared. Variable names must be unique within a query batch or stored procedure.
here id is the name of the number i used to identify the image associated with it.
On the next button click i again get this exception and then the program crashes with this error
Invalid attempt to call Read when reader is closed.
Pls help...
ProgramFOX at 10-Apr-13 9:06am
What's your code to add the '@id' parameter?
JPais at 10-Apr-13 9:30am
My code...
query = "INSERT INTO detail2 VALUES(@id,@pic)"
cmd.Parameters.AddWithValue("@id", Label33.Text)
Dim ms As New MemoryStream()
PictureBox7.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim imgData = ms.ToArray
Dim data1 As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@pic", SqlDbType.Image)
p.Value = data1
MessageBox.Show("Tag & Image has been saved", "Save", MessageBoxButtons.OK)
ProgramFOX at 10-Apr-13 9:36am
I can't find something that throws an error, I'm sorry.
nawabahmad at 10-Jun-13 5:48am
Please send me the code to capture image from webcam and store in sql database
mail id:-

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

  Print Answers RSS
0 Maciej Los 600
1 OriginalGriff 455
2 Sascha Lefévre 289
3 Richard Deeming 195
4 Abhinav S 190
0 Sergey Alexandrovich Kryukov 9,693
1 OriginalGriff 9,185
2 Peter Leow 5,112
3 Kornfeld Eliyahu Peter 3,373
4 Maciej Los 2,876

Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 19 Feb 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100