"System.OutOfMemoryException: 'Out of memory.'" when reading image from SQL Server

jumexmango 136 Reputation points
2021-12-08T18:50:06.227+00:00

I have images assigned to every button in my VB.NET form, the images come from SQL Server. The data type is varbinary(MAX). The platform is 64bit. I'm thinking it might have to do with not disposing properly, but I'm not sure since I'm new to coding. The error only appears when more than one image is read. The weird part is that I have a different form in the same project with similar code that does work.

This is my code:

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
 Using con As New SqlConnection("con string")  
        Dim sql As String = "SELECT * FROM Inventario WHERE ID=@ID"  
        Using cmd As New SqlCommand(sql, con)  
            cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 3  
            con.Open()  
            Using myreader As SqlDataReader = cmd.ExecuteReader()  
                If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then  
                    Boton3.Text = myreader("Articulo")  
                    Boton3.Enabled = myreader("ONOFF")  
                    Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())  
                    Using ms As New MemoryStream(ImgSql)  
                        Boton3.BackgroundImage = Image.FromStream(ms)  
                        con.Close()  
                    End Using  
                Else  
                    Boton3.Text = myreader("Articulo")  
                    Boton3.BackgroundImage = Nothing  
                    Boton3.Enabled = myreader("ONOFF")  
                End If  
            End Using  
        End Using  
    End Using  
    Using con As New SqlConnection("con string;")  
        Dim sql As String = "SELECT * FROM Inventario WHERE ID=@ID"  
        Using cmd As New SqlCommand(sql, con)  
            cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = 4  
            con.Open()  
            Using myreader As SqlDataReader = cmd.ExecuteReader()  
                If myreader.Read() AndAlso Not DBNull.Value.Equals(myreader("Imagen")) Then  
                    Boton4.Text = myreader("Articulo")  
                    Boton4.Enabled = myreader("ONOFF")  
                    Dim ImgSql() As Byte = DirectCast(myreader("Imagen"), Byte())  
                    Using ms As New MemoryStream(ImgSql)  
                        Boton4.BackgroundImage = Image.FromStream(ms)  
                        con.Close()  
                    End Using  
                Else  
                    Boton4.Text = myreader("Articulo")  
                    Boton4.BackgroundImage = Nothing  
                    Boton4.Enabled = myreader("ONOFF")  
                End If  
            End Using  
        End Using  
    End Using  

@Xingyu Zhao-MSFT

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,690 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,569 questions
{count} votes

Accepted answer
  1. jumexmango 136 Reputation points
    2021-12-08T21:22:45.443+00:00

    I resolved this issue by simply not using buttons. Instead I used pictureboxes as buttons and that resolved the issue. Im guesssing the problem is that buttons don't allow as much memory as pictureboxes. Can anyone confirm this?


0 additional answers

Sort by: Most helpful