Click here to Skip to main content
15,893,487 members

i am getting an exception while i am insertin the picture through picture box into my database

Pratik65 asked:

Open original thread
CSS
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:



VB
Imports System.IO
02
Imports System.Data
03
Imports System.Data.SqlClient
04
Imports System.Data.SqlTypes
05
Public Class Form1
06
    Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=ImagesStore;Integrated Security=True;Pooling=False")
07
    Dim cmd As SqlCommand
08
 
09
    Private mImageFile As Image
10
    Private mImageFilePath As String
11
 
12
 Private Sub BtnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpen.Click
13
        Dim imge As String
14
        OpenFileDialog1.Title = "Set Image File"
15
        OpenFileDialog1.Filter = "Bitmap Files|*.bmp" & _
16
            "|Gif Files|*.gif|JPEG Files|*.jpg"
17
        OpenFileDialog1.DefaultExt = "bmp"
18
        OpenFileDialog1.FilterIndex = 1
19
        OpenFileDialog1.FileName = ""
20
        OpenFileDialog1.ShowDialog()
21
 
22
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
23
            Exit Sub
24
        End If
25
        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
26
            imge = OpenFileDialog1.FileName
27
            PictureBox1.Image = System.Drawing.Bitmap.FromFile(imge)
28
        End If
29
        Dim sFilePath As String
30
        sFilePath = OpenFileDialog1.FileName
31
        If sFilePath = "" Then Exit Sub
32
 
33
        If System.IO.File.Exists(sFilePath) = False Then
34
            Exit Sub
35
        Else
36
            txtImageFile.Text = sFilePath
37
            mImageFilePath = sFilePath
38
        End If
39
    End Sub
40
 
41
 Private Sub BtnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
42
        Try
43
            If (Me.txtImageFile.Text = String.Empty Or Me.txtTitle.Text =
44
                String.Empty) Then
45
                MessageBox.Show("Complete both form fields prior to submitting",
46
                "Missing Values", _
47
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
48
                Exit Sub
49
            End If
50
        Catch ex As Exception
51
            MessageBox.Show(ex.Message.ToString(), "File Test Error")
52
        End Try
53
 
54
        Dim fs As FileStream = New FileStream(mImageFilePath.ToString(),
55
        FileMode.Open)    \\I am getting the exception in this line \\
56
 
57
        Dim img As Byte() = New Byte(fs.Length) {}
58
        fs.Read(img, 0, fs.Length)
59
        fs.Close()
60
 
61
        Dim sSQL As String = "INSERT INTO ImagesStore(OriginalPath,ImageData)VALUES(@op,@id)"
62
        cmd = New SqlCommand(sSQL, conn)
63
 
64
        cmd.Parameters.AddWithValue("@op", (img))
65
        cmd.Parameters.AddWithValue("@id", (txtTitle.Text))
66
 
67
        Try
68
            conn.Open()
69
            cmd.ExecuteNonQuery()
70
            conn.Close()
71
            MessageBox.Show("Query executed.", "Image Load")
72
        Catch ex As Exception
73
            MessageBox.Show(ex.Message.ToString(), "Data Error")
74
            Exit Sub
75
        End Try
76
    End Sub
Tags: Visual Basic

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900