Click here to Skip to main content
Rate this: bad
good
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
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 31-Mar-13 1:36am
JPais1.4K
Comments
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
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
Try:
PictureBox1.BackgroundImage.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Hope this helps.
  Permalink  
Rate this: bad
good
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??
  Permalink  
Comments
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 & "%'"
Try
DataGridView1.DataSource = Nothing
OpenConn()
 
Dim da As SqlDataAdapter = New SqlDataAdapter("", sql)
da.SelectCommand = New SqlCommand(query, sql)
Dim ds As New DataSet
da.Fill(ds)
If ds.Tables.Count > 0 Then
DataGridView1.DataSource = ds.Tables(0).DefaultView
End If
sql.Close()
Catch ex As Exception
MsgBox(ex.Message)
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
   
Hi
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
ms.Close()
Dim data1 As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@pic", SqlDbType.Image)
p.Value = data1
cmd.Parameters.Add(p)
ExecuteDataBaseQuery(query)
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:- nawabjob@gmail.com

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

  Print Answers RSS
0 OriginalGriff 270
1 Sergey Alexandrovich Kryukov 185
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 19 Feb 2014
Copyright © CodeProject, 1999-2014
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