Results 1 to 7 of 7

Thread: [RESOLVED] Problem with Save Image

  1. #1

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Resolved [RESOLVED] Problem with Save Image

    i have problem when save image use MS Access Database.
    field name is pict -> OLE OBJECT

    hire my code when save image :
    vb.net Code:
    1. 'simpan file gambar
    2.             Dim img As Image = ucPhoto.getGambar
    3.  
    4.  
    5.             If img IsNot Nothing Then
    6.  
    7.                 Dim imgPar As New OleDbParameter
    8.                 Dim fs As FileStream
    9.                 Dim sfname As String = ucPhoto.FileName
    10.  
    11.                 fs = New FileStream(sfname, FileMode.Open, FileAccess.Read)
    12.                 Dim picByte As Byte() = New Byte(CInt(fs.Length - 1)) {}
    13.  
    14.                 fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
    15.  
    16.                 fs.Close()
    17.  
    18.  
    19.                 imgPar.OleDbType = OleDbType.Binary
    20.                 imgPar.ParameterName = "pict"
    21.                 imgPar.Value = picByte
    22.  
    23.             End If

    when i run that code is not problem. but when i run code to display image i get error:
    vb.net Code:
    1. If dr.Item("pict") IsNot Nothing Then
    2.                     Dim fsImage As New IO.FileStream("img.jpg", IO.FileMode.Create)
    3.                     Dim blob As Byte() = DirectCast(dr.Item("pict"), Byte())
    4.  
    5.                     fsImage.Write(blob, 0, blob.Length)
    6.                     fsImage.Close()
    7.                     fsImage = Nothing
    8.  
    9.                     ucPhoto.setImage(Image.FromFile("img.jpg"))
    10.  
    11.                 End If

    error at line :
    Code:
    Dim blob As Byte() = DirectCast(dr.Item("gambar"), Byte())
    error message :
    Code:
    Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'
    thank you

  2. #2

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Problem with Save Image

    I try to check again my code. and it turns out dr.Item field ("pict") NULL.
    means that there is fail when saving the image. where lies the fault?
    I show the following sql code:
    vb.net Code:
    1. scmd.CommandText = "insert into inv_t_products (prod_id,prod_name,unit,unitprice,id_group_prod,supp_id,pict) " & _
    2.                         "values(@prodid,@prodname,@unit,@unitprice,@idgroup,@idsupp,@pict)"

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

    Re: Problem with Save Image

    Your "save" code doesn't actually save anything.

    Also, there's no point testing your DataRow or DataReader field for Nothing because it will never be Nothing.
    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

  4. #4

    Thread Starter
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Problem with Save Image

    now I've managed to save and display the data, there are a few lines of code is wrong so that images can not be saved.

    but now I have a problem when displaying data, there is an error message when I view the picture for the second time.
    error description :
    Code:
    The process cannot access the file 'img.jpg' because it is being used by another process

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

    Re: Problem with Save Image

    Perhaps this might help

    The MS-Access database has a table 'table1' with an OLE Object field 'ImageField' to store images.

    Code:
    Public Class Form1
        ' Resides in the Debug folder
        Private FileName As String = "untitled.bmp"
    
        ' MDB resides in the Debug folder
        Private ConnectionString As String = _
              <Text>
                Provider=Microsoft.Jet.OLEDB.4.0;
                Data Source=db.mdb;
             </Text>.Value
    
        Private Connection As New OleDb.OleDbConnection(ConnectionString)
    
        Private da As New OleDb.OleDbDataAdapter("select ImageField from table1", _
                                                 ConnectionString)
        Private ds As New DataSet()
        Private cb As New OleDb.OleDbCommandBuilder(da)
    
        Private Sub Form1_Load() Handles MyBase.Load
    
            da.Fill(ds)
    
            ' simple demo only loads one image
            If ds.Tables(0).Rows.Count = 1 Then
                Exit Sub
            End If
    
            Dim br As New IO.BinaryReader(New IO.FileStream(FileName, _
                                              IO.FileMode.Open, _
                                              IO.FileAccess.Read, _
                                              IO.FileShare.Read))
    
            Dim row As DataRow
            row = ds.Tables(0).NewRow
            row.Item("ImageField") = br.ReadBytes(CInt(br.BaseStream.Length))
            ds.Tables(0).Rows.Add(row)
            da.Update(ds)
        End Sub
        Private Sub Form1_Shown() Handles Me.Shown
            ' Load First row image
            PictureBox1.Image = Image.FromStream( _
                New IO.MemoryStream(CType(ds.Tables(0).Rows(0).Item("ImageField"), Byte())))
        End Sub
    End Class

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

    Re: Problem with Save Image

    Quote Originally Posted by Tengkorak View Post
    now I've managed to save and display the data, there are a few lines of code is wrong so that images can not be saved.

    but now I have a problem when displaying data, there is an error message when I view the picture for the second time.
    error description :
    Code:
    The process cannot access the file 'img.jpg' because it is being used by another process
    The Image.FromFile method locks the file. To unlock it, you must dispose the Image.

    Do you really need to create the files at all? Can you not just create the Image object from a MemoryStream, as demonstrated by KI? You might also check out my thread on saving and loading images in databases in the VB.NET CodeBank forum.
    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
    Addicted Member Tengkorak's Avatar
    Join Date
    Nov 2006
    Posts
    240

    Re: Problem with Save Image

    thank you for the solution given, now I've managed to save and display images.

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