Results 1 to 11 of 11

Thread: problem inserting image from database

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    Post 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

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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)

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    Re: problem inserting image from database

    Quote Originally Posted by jmcilhinney View Post
    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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'.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    Post Re: problem inserting image from database

    Quote Originally Posted by jmcilhinney View Post
    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.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    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.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Dec 2011
    Posts
    33

    Re: problem inserting image from database

    Quote Originally Posted by jmcilhinney View Post
    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
  •  



Click Here to Expand Forum to Full Width