• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Out Of Memory Error

Hi
I am attempting to load an image from SQL database when I get an out of memory exception! How would I correct this?
JoeBo747

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myconnection As New Data.SqlClient.SqlConnection(My.Settings.Transport_test)
        Try
 
            Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
            cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
            cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
            myconnection.Open()
            Dim barrImg() As Byte = CType(cmdSelect.ExecuteScalar(), Byte())
            Dim strfn As String = Convert.ToString(DateTime.Now.ToFileTime())
            Dim fs As FileStream = New FileStream(strfn, FileMode.CreateNew, FileAccess.Write)
            fs.Write(barrImg, 0, barrImg.Length)
            fs.Flush()
            fs.Close()
 
            PictureBox1.Image = Image.FromFile(strfn)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
 
        Finally
            myconnection.Close()
 
        End Try

Open in new window

0
JoeBo747
Asked:
JoeBo747
  • 12
  • 8
1 Solution
 
silemoneCommented:
dispose of all objects that aren't needed or make them= nothing

otherwise, it may be a computer setting that needs to be updated...
0
 
JackOfPHCommented:
Try my code... post back if you have questions...

Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()

Dim reader As SqlClient.SqlDataReader
Dim cmd As New SqlClient.SqlCommand
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Do while reader.read

Dim barrImg() As Byte = Reader.Item("POD_Sign")
Dim ms As New IO.MemoryStream(barrImg())
PictureBox1.Image = Image.FromStream(ms)
ms.close
ms.dispose
Loop
0
 
JackOfPHCommented:
Here try this one... This should be working... Post if you have questions...

Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()

Dim reader As SqlClient.SqlDataReader

reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)

Do while reader.read

Dim barrImg() As Byte = Reader.Item("POD_Sign")
Dim ms As New IO.MemoryStream(barrImg())
PictureBox1.Image = Image.FromStream(ms)
ms.close
ms.dispose
Loop
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
JoeBo747Author Commented:
Hi JackOfPH

Thanks for the advice but your code throughs an error at line:

Dim ms As New IO.MemoryStream(barrImg()) <<< error here

'Number of indices is less than the number of dimensions of the indexed array.'

Please advice
JoeB0747

0
 
JackOfPHCommented:
Sorry for the late reply, Try this code instead.

Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()

Dim reader As SqlClient.SqlDataReader

reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)

Do while reader.read

Dim barrImg() As Byte = Reader.Item("POD_Sign")
Dim ms As New IO.MemoryStream(barrImg)
PictureBox1.Image = Image.FromStream(ms)
ms.close
ms.dispose
Loop
0
 
JoeBo747Author Commented:
Hi JackOfPH

Thanks for the reply, I have tried the code and I am getting an error

System.ArgumentException was caught
  Message="Parameter is not valid."
  Source="System.Drawing"
  StackTrace:
       at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
       at System.Drawing.Image.FromStream(Stream stream)
     
PictureBox1.Image = Image.FromStream(ms) <<<<<< error here

Regards
JoeBo747
0
 
JackOfPHCommented:
Can you post the value of barrImg?

Try this code...
Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()
 
Dim reader As SqlClient.SqlDataReader
 
reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)
 
Do while reader.read
If Reader.hasrows = true then
Dim barrImg() As Byte = Reader.Item("POD_Sign")
Dim ms As New IO.MemoryStream(barrImg)
PictureBox1.Image = Image.FromStream(ms)
ms.close
ms.dispose
End if
Loop

Open in new window

0
 
JackOfPHCommented:
Try the above code or this one...


Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()
 
Dim reader As SqlClient.SqlDataReader
 
reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)
 
Do while reader.read
If Reader.hasrows = true then
If not Reader.Item("POD_Sign") is nothing then
         Dim barrImg() As Byte = Reader.Item("POD_Sign")
         Dim ms As New IO.MemoryStream(barrImg)
         PictureBox1.Image = Image.FromStream(ms)
         ms.close
         ms.dispose
End If
End If
Loop

Open in new window

0
 
JoeBo747Author Commented:
Hi JackOfPH

Thanks for sticking with me on this one, none of the above code versions will work! I have attache the contents of barrImg in a txt file, hope this will help resolve the issue.

Regards
JoeBo747
bytes.txt
0
 
JackOfPHCommented:
What is the datatype of the POD_Sign?
0
 
JoeBo747Author Commented:
Hi JackOfPH

The datatype is 'image'  when viewed it shows '<Binary data>' in the field!

The image is saved using the following function.

Public Function StrToByteArray(ByVal str As String) As Byte()
        Dim encoding As New System.Text.ASCIIEncoding()
        Return encoding.GetBytes(str)
    End Function

Regards
JoeBo747
0
 
JackOfPHCommented:
I suggest you to use byte instead of image datatype...
0
 
JackOfPHCommented:
And here is how you convert it to byte





 
Public Function imageToByteArray(ByVal objPicture As image) As Byte()
       Dim ms As New System.IO.MemoryStream
            objPicture.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
            ms.Seek(0, IO.SeekOrigin.Begin)
            Dim data(ms.Length - 1) As Byte
            ms.Read(data, 0, ms.Length)
            ms.Close()
            ms.dispose
 
           return data
End Function 

Open in new window

0
 
JackOfPHCommented:
using the above code, you can use the code I gave you for retrieving, just bare in mind that you have to use byte instead of image datatype.
0
 
JackOfPHCommented:
???
0
 
JoeBo747Author Commented:
Hi JackOfPH

Sorry I have not been back to you I was out of the office all day yesterday, I will check it out now and get back straight away.

Regards
Joe
0
 
JoeBo747Author Commented:
Hi JackOfPH

 I suggest you to use byte instead of image datatype...

I am using sql 2000, do you mean to use the binary data type to store the image I am converting using strfrombytearray.  The original image is read from an xml node which is in rle4 format please see attached xml node. Should I use another method to save the xml image?

Regards
JoeBo

signature.txt
0
 
JackOfPHCommented:
Sorry, for a long reply...

Can you try this code?


Dim cmdSelect As SqlCommand = New SqlCommand("SELECT POD_Sign FROM M_POD where JobID=@ID", myconnection)
cmdSelect.Parameters.Add("@ID", SqlDbType.Int, 4)
cmdSelect.Parameters("@ID").Value = Me.editID.Text
 
myconnection.Open()
 
Dim reader As SqlClient.SqlDataReader
 
reader = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection)
 
Do while reader.read
If Reader.hasrows = true then
If not Reader.Item("POD_Sign") is nothing then
         Dim barrImg() As Byte = StrToByteArray(Reader.Item("POD_Sign"))
         Dim ms As New IO.MemoryStream(barrImg)
         PictureBox1.Image = Image.FromStream(ms)
         ms.close
         ms.dispose
End If
End If
Loop

Open in new window

0
 
JoeBo747Author Commented:
Hi FrankOfPH
I am sorry about the delay in returning to this question but I am not always in the office where I can get access to VS to trial your suggestions. I have trailed your alternative suggestion again without luck and have come to the conclusion that the problem must lie with the saved image. Would you clarify the following point  for me?
I am saving the image to sql server 2000 the field type is currently set to an image, you have suggested changing this to an byte instead of image datatype,  byte is not available on sql 2000 what type should I use instead.

Regards
JoeBo
0
 
JackOfPHCommented:
Sorry, it should be binary instead of byte....

Jack
0
 
JoeBo747Author Commented:
Hi JackOfPH
I am sorry it has taken so long for me to respond,  I have changed the type tobinary and that has corrected the errors I was receiving and I have also changed the method to write the file to the server to
Dim bytes As Byte() = Convert.FromBase64String(stringFromXml)
This with your methods to load the file has worked for me. Thanks for sticking with me on this.
Regards
JoeBo747
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 12
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now