|
-
May 20th, 2012, 02:18 AM
#1
Thread Starter
Member
problem inserting image from database
this is my code can't insert the image from the database
Code:
Dim str As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jayhson\documents\visual studio 2010\Projects\CBAS\CBAS\data_file.mdf;Integrated Security=True;User Instance=True"
Dim con As New SqlConnection(str)
cn.Open()
Dim sql As String = "INSERT INTO tblData VALUES(@Bcode,@ItemName,@Description,@BcodeImage)"
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@Bcode", txtBcode.Text)
cmd.Parameters.AddWithValue("@ItemName", txtItemName.Text)
cmd.Parameters.AddWithValue("@Description", rtdescription.Text)
Dim ms As New MemoryStream()
PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@BcodeImage", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
cmd.ExecuteNonQuery()
MessageBox.Show("record has been saved", "Save", MessageBoxButtons.OK)
con.Close()
txtBcode.Text = ""
txtItemName.Text = ""
rtdescription.Text = ""
error
from this code: PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
Value cannot be null.
Parameter name: encoder
-
May 20th, 2012, 06:57 PM
#2
Re: problem inserting image from database
I would try the following, change the image type as needed.
PictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
-
May 23rd, 2012, 03:10 AM
#3
Thread Starter
Member
Re: problem inserting image from database
i try that one see my code, i got an error the msgbox come up but when i try to check from the database nothing in there and also i got this
"ExecuteNonQuery requires an open and available Connection. The connection's current state is closed."
cmd.ExecuteNonQuery()
Private Sub btnDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDB.Click
If txtBcode.Text = "" Then
MsgBox("Please put a bar code number and generate a barcode", vbCritical, "Error")
Else
Dim str As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jayhson\documents\visual studio 2010\Projects\CBAS\CBAS\data_file.mdf;Integrated Security=True;User Instance=True"
Dim con As New SqlConnection(str)
cn.Open()
Dim sql As String = "INSERT INTO tblData VALUES(@Bcode,@ItemName,@Description,@BcodeImage)"
Dim cmd As New SqlCommand(sql, con)
cmd.Parameters.AddWithValue("@Bcode", txtBcode.Text)
cmd.Parameters.AddWithValue("@ItemName", txtItemName.Text)
cmd.Parameters.AddWithValue("@Description", rtdescription.Text)
Dim ms As New MemoryStream()
PictureBox2.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
Dim data As Byte() = ms.GetBuffer()
Dim p As New SqlParameter("@BcodeImage", SqlDbType.Image)
p.Value = data
cmd.Parameters.Add(p)
MessageBox.Show("record has been saved", "Save", MessageBoxButtons.OK)
cmd.ExecuteNonQuery()
cn.Close()
txtBcode.Text = ""
txtItemName.Text = ""
rtdescription.Text = ""
End If
End Sub
-
May 23rd, 2012, 04:21 AM
#4
Re: problem inserting image from database
You'll give yourself a forehead slap when you see this:
Code:
Dim con As New SqlConnection(str)
cn.Open()
Dim sql As String = "INSERT INTO tblData VALUES(@Bcode,@ItemName,@Description,@BcodeImage)"
Dim cmd As New SqlCommand(sql, con)
This is an example of why you should use "proper" variable names.
-
May 23rd, 2012, 06:05 AM
#5
Thread Starter
Member
Re: problem inserting image from database
 Originally Posted by jmcilhinney
You'll give yourself a forehead slap when you see this:
Code:
Dim con As New SqlConnection(str)
cn.Open()
Dim sql As String = "INSERT INTO tblData VALUES(@Bcode,@ItemName,@Description,@BcodeImage)"
Dim cmd As New SqlCommand(sql, con)
This is an example of why you should use "proper" variable names.
I didn't see this one thanks buddy,
from my code upstair wont work the image i didnt see the image insert into my database only the data. I check the table is not there
-
May 23rd, 2012, 06:40 AM
#6
Re: problem inserting image from database
What exactly is 'data' before you save? If you query the database and get that record, what is the value in that column?
Also, if you have control of the database, you should be using type 'varbinary' rather than 'image'.
-
May 23rd, 2012, 07:02 AM
#7
Thread Starter
Member
Re: problem inserting image from database
 Originally Posted by jmcilhinney
What exactly is 'data' before you save? If you query the database and get that record, what is the value in that column?
Also, if you have control of the database, you should be using type 'varbinary' rather than 'image'.
the data is the one who the user input from the textbox
i change the image type to varbinary, after i run the program this is the error during i hit the submit button
String or binary data would be truncated.
The statement has been terminated.
-
May 23rd, 2012, 07:11 AM
#8
Re: problem inserting image from database
Then you haven't specified the maximum size to be high enough. Just line varchar, varbinary requires you to specify the maximum size of a value. As that is the maximum size, you can't save a value larger than that. If you need to save data of a particular size then you must specify at least that size in the database.
-
May 23rd, 2012, 07:24 AM
#9
Thread Starter
Member
Re: problem inserting image from database
still the same problem i put varbinary(255)
String or binary data would be truncated.
The statement has been terminated.
-
May 23rd, 2012, 07:30 AM
#10
Re: problem inserting image from database
So what you're saying is that there is no way possible that you will ever be saving more than 255 bytes in that column, right? So what size is the array that you're trying to save?
-
May 23rd, 2012, 07:38 AM
#11
Thread Starter
Member
Re: problem inserting image from database
 Originally Posted by jmcilhinney
So what you're saying is that there is no way possible that you will ever be saving more than 255 bytes in that column, right? So what size is the array that you're trying to save?
thanks you give me an idea on this
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|