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

New Topic/Question
Reply



MultiQuote






|