?
Solved

Out Of Memory Error

Posted on 2009-05-02
21
Medium Priority
?
216 Views
Last Modified: 2012-05-06
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
Comment
Question by:JoeBo747
  • 12
  • 8
21 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 24285645
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24292365
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24292373
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:JoeBo747
ID: 24293924
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24300995
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 24302193
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302248
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24302538
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 24303212
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24310525
What is the datatype of the POD_Sign?
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 24312467
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24312524
I suggest you to use byte instead of image datatype...
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24312565
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24312578
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24333468
???
0
 
LVL 1

Author Comment

by:JoeBo747
ID: 24333868
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 24334040
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
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24352369
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
 
LVL 1

Author Comment

by:JoeBo747
ID: 24366714
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
 
LVL 15

Accepted Solution

by:
JackOfPH earned 2000 total points
ID: 24370703
Sorry, it should be binary instead of byte....

Jack
0
 
LVL 1

Author Closing Comment

by:JoeBo747
ID: 31577150
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question