title: Uploading/Retrieving an image of any format into SQL server data through vb 2010
body: <p>I want to upload an image to data base, I found lots of code over the internet but non of them worked for me.  I am browsing images with button Browse of format(png, gif, jpeg and bmp) and after then I want to upload these types of images to the data base with button Save.<br>
  For retrieving them back i use another button load. Can you please guide me how would i do this.<br>
  This is my coding Tell me where am I wrong.     </p>
  
  <pre><code>Imports System.Data.SqlClient
  Imports System.IO
  Imports System.Drawing.Image
  
  Public Class Employee
  
  Dim myImage As Image
  Dim imgMemoryStream As IO.MemoryStream = New IO.MemoryStream()
  Dim imgByteArray As Byte() = Nothing
  Dim con As SqlConnection = New SqlConnection("Data Source=CILENTEYEZ-PC\CILENTEYEZ;Initial Catalog=Keeper;Integrated Security=True")
  Dim cmd As SqlCommand
  Dim myDA As SqlDataAdapter
  Dim myDataSet As DataSet
  Dim dr As SqlDataReader
  
  ' From Here This is the Code for Insertion.
  
  Private Sub BrowsePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BrowsePic.Click
  
      Dim fd As OpenFileDialog = New OpenFileDialog()
  
      fd.Title = "Select your Image."
      fd.InitialDirectory = "C:\"
      fd.Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg|PNGs|*.png"
      fd.RestoreDirectory = False
  
      If fd.ShowDialog() = DialogResult.OK Then
          PictureBox.ImageLocation = fd.FileName
      ElseIf fd.FileName.Contains("jpeg") Or fd.FileName.Contains("jpg") Then
  
          myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg)
          imgByteArray = imgMemoryStream.GetBuffer()
  
      ElseIf fd.FileName.Contains("png") Then
  
          myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Png)
          imgByteArray = imgMemoryStream.GetBuffer()
  
      ElseIf fd.FileName.Contains("gif") Then
  
          myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Gif)
          imgByteArray = imgMemoryStream.GetBuffer()
  
      ElseIf fd.FileName.Contains("bmp") Then
  
          myImage.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Bmp)
          imgByteArray = imgMemoryStream.GetBuffer()
  
      End If
  
  End Sub
  
  Private Sub Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save.Click
  
  cmd = New SqlCommand("Insert Into Employee Values('" &amp; SR_CodeTextBox.Text.Trim() &amp; "','" &amp; NameTextBox.Text.Trim() &amp; "','" &amp; CNICTextBox.Text.Trim() &amp; "','" &amp; Date_of_BirthDateTimePicker.Text &amp; "','" &amp; AgeTextBox.Text.Trim() &amp; "','" &amp; AddressTextBox.Text.Trim() &amp; "',@ imgByteArray ,'" &amp; Mobile_NumberTextBox.Text.Trim() &amp; "')", con)
          If con.State = ConnectionState.Closed Then con.Open()
          cmd.ExecuteNonQuery()
          con.Close()
          MessageBox.Show("New Employee Record is Added.", "Message", MessageBoxButtons.OK)
  
  End Sub
  
  ' From Here This is the Code for Eiditing.
  
  Private Sub Go_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go.Click
  
      cmd = New SqlCommand("Select * from Employee where SR_Code = '" &amp; SearchBox.Text.Trim() &amp; "'", con)
  
      If SearchBox.Text = "" Then
          MsgBox("Please enter SR Code first.")
  
      Else
  
          Try
  
              If con.State = ConnectionState.Closed Then con.Open()
              dr = cmd.ExecuteReader()
  
              If dr.HasRows = True Then
  
                  dr.Read()
  
                  Edit_SR_CodeTextBox.Text = dr.Item("SR_Code")
                  Edit_NameTextBox.Text = dr.Item("Name")
                  Edit_CNICTextBox.Text = dr.Item("CNIC")
                  Edit_Date_of_BirthDateTimePicker.Text = dr.Item("Date_of_Birth")
                  Edit_AgeTextBox.Text = dr.Item("Age")
                  Edit_AddressTextBox.Text = dr.Item("Address")
                  imgByteArray = dr.Item("Picture")
                  Edit_Mobile_NumberTextBox.Text = dr.Item("Mobile_Number")
  
                  imgMemoryStream = New IO.MemoryStream(imgByteArray)
                  myImage = Drawing.Image.FromStream(imgMemoryStream)
                  PictureBox.Image = myImage
  
                  MsgBox("Record Retrieved.")
                  con.Close()
  
              ElseIf dr.Read = False Then
                  MsgBox("Please enter a valid SR Code to load data.")
                  con.Close()
              End If
  
          Catch ex As Exception
              MessageBox.Show(ex, "Message")
  
          End Try
  
      End If
  
  End Sub
  </code></pre>