3 Replies - 386 Views - Last Post: 06 December 2012 - 11:26 AM Rate Topic: -----

#1 pratik65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 10-September 12

i am getting an exception while inserting the picture into the databse

Posted 06 December 2012 - 10:59 AM

i am getting the following exception while i am inserting a picture into database through a picturebox

System.IO.IOException was unhandled
Message=The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg' because it is being used by another process.


i have tried the following code:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Public Class Form1
    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=ImagesStore;Integrated Security=True;Pooling=False")
    Dim cmd As SqlCommand

    Private mImageFile As Image
    Private mImageFilePath As String

 Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
        Dim imge As String
        OpenFileDialog1.Title = "Set Image File"
        OpenFileDialog1.Filter = "Bitmap Files|*.bmp" & _
            "|Gif Files|*.gif|JPEG Files|*.jpg"
        OpenFileDialog1.DefaultExt = "bmp"
        OpenFileDialog1.FilterIndex = 1
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.ShowDialog()

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        End If
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            imge = OpenFileDialog1.FileName
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(imge)
        End If
        Dim sFilePath As String
        sFilePath = OpenFileDialog1.FileName
        If sFilePath = "" Then Exit Sub

        If System.IO.File.Exists(sFilePath) = False Then
            Exit Sub
        Else
            txtImageFile.Text = sFilePath
            mImageFilePath = sFilePath
        End If
    End Sub

 Private Sub BtnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
        Try
            If (Me.txtImageFile.Text = String.Empty Or Me.txtTitle.Text =
                String.Empty) Then
                MessageBox.Show("Complete both form fields prior to submitting",
                "Missing Values", _
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "File Test Error")
        End Try

        [b]Dim fs As FileStream = New FileStream(mImageFilePath.ToString(),
        FileMode.Open)[/b]     [b]\\I am getting the exception in this line \\ [/b]

        Dim img As Byte() = New Byte(fs.Length) {}
        fs.Read(img, 0, fs.Length)
        fs.Close()

        Dim sSQL As String = "INSERT INTO ImagesStore(OriginalPath,ImageData)VALUES(@op,@id)"
        cmd = New SqlCommand(sSQL, conn)

        cmd.Parameters.AddWithValue("@op", (img))
        cmd.Parameters.AddWithValue("@id", (txtTitle.Text))

        Try
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
            MessageBox.Show("Query executed.", "Image Load")
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString(), "Data Error")
            Exit Sub
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: i am getting an exception while inserting the picture into the databse

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 767
  • Joined: 19-October 11

Re: i am getting an exception while inserting the picture into the databse

Posted 06 December 2012 - 11:12 AM

Before you use image somewhere else, you need to dispose it.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9498
  • View blog
  • Posts: 35,845
  • Joined: 12-June 08

Re: i am getting an exception while inserting the picture into the databse

Posted 06 December 2012 - 11:19 AM

Is the picture open in a viewer or editor?
Was This Post Helpful? 0
  • +
  • -

#4 pratik65  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 10-September 12

Re: i am getting an exception while inserting the picture into the databse

Posted 06 December 2012 - 11:26 AM

ye the picture is open in the picturebox on the form so that the viewer can see it and upload the image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1