title: Can't Retrieve image from my SQL Server database using vb.net
body: <p>I created a table in my database which stores the user's information and his/her picture. My picture column has an <code>image</code> data type.</p>
  
  <p>I completed my registration form and successfully added data to my database using this code:</p>
  
  <pre><code> Private Sub cmdRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdRegister.Click
  
   comm.CommandText = "insert into Users(user_id, userNo_id, username, password, last_name, middle_name, first_name, course, section, position, address, birthday,picture) values (@field1,@field2,@field3,@field4,@field5, @field6, @field7,@field8, @field9, @field10, @field11,@field12, @photo)"
   comm.Connection = con
  
   Dim Password As String = String.Empty
   Dim birthday As String
   Dim ms As New MemoryStream
  
   PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
  
   Dim data As Byte() = ms.GetBuffer
  
   Dim p As New SqlParameter("@photo", SqlDbType.Image)
   p.Value = data
   birthday = cmbMonth.Text + " " + cmbDay.Text + ", " + txtYear.Text
  
   con.Open()
   Password = Encrypt(txtPassword.Text.Trim())
   With comm
       .Parameters.AddWithValue("@field1", txtUserID.Text)
       .Parameters.AddWithValue("@field2", txtUserNo_id.Text)
       .Parameters.AddWithValue("@field3", txtUsername.Text)
       .Parameters.AddWithValue("@field4", Password)
       .Parameters.AddWithValue("@field5", txtLastName.Text)
       .Parameters.AddWithValue("@field6", txtMiddleName.Text)
       .Parameters.AddWithValue("@field7", txtFirstName.Text)
       .Parameters.AddWithValue("@field8", txtCourse.Text)
       .Parameters.AddWithValue("@field9", txtSection.Text)
       .Parameters.AddWithValue("@field10", cmbPosition.Text)
       .Parameters.AddWithValue("@field11", txtAddress.Text)
       .Parameters.AddWithValue("@field12", birthday)
       .Parameters.Add(p)
  
   End With
  
   comm.ExecuteNonQuery()
   comm.Dispose()
   MsgBox("Records Successfully Saved")
   clear()
   con.Close()
  </code></pre>
  
  <p>But when I tried to retrieve my data especially the picture.. I get an error "Out of Memory"</p>
  
  <p>This is my code trying to retrieve my data and image..</p>
  
  <pre><code> Sub fillDataFields()
      Dim mid As String
      Dim last As String
      Dim first As String
  
      con.Open()
  
      comm.CommandText = "Select last_name,middle_name,first_name,course, section, address, " &amp; _
                     "birthday, picture from Users where user_id like @uid"
      comm.Connection = con
      comm.Parameters.AddWithValue("@uid", "%" &amp; frmUsers.ListView1.SelectedItems(0).Text &amp; "%")
  
      dr = comm.ExecuteReader
  
      While (dr.Read())
          last = (dr("last_name"))
          mid = (dr("middle_name"))
          first = (dr("first_name"))
          txtCourse.Text = (dr("course"))
          txtSection.Text = (dr("section"))
          richtxtAddress.Text = (dr("address"))
          txtBirthday.Text = (dr("birthday"))
          txtName.Text = last + ", " + first + " " + mid
  
          Dim imageData As Byte() = DirectCast(dr("picture"), Byte())
  
          If Not imageData Is Nothing Then
              Using ms As New MemoryStream(imageData, 0, imageData.Length)
                  ms.Write(imageData, 0, imageData.Length)
                  PictureBox1.BackgroundImage = Image.FromStream(ms, True)
              End Using
          End If
      End While
  
      con.Close()
      dr.Close()
      comm.Dispose()
  End Sub
  </code></pre>
  
  <p>Can you please help me? </p>