Click here to Skip to main content
14,970,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi this is Ashwani and I am getting the above mentioned error while running the program, plz help me....
here's my code:-

//--------------------code-----------------------//

VB
Imports System
Imports System.Net
Imports System.Drawing
Imports System.Collections
Imports System.Componentmodel
Imports System.windows.Forms
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Drawing.Imaging
Imports System.IO
Imports Scanning.TwainLib
Imports System.Data.SqlClient
Imports System.IO.Directory
Imports System.Threading

Public Class hbet
    Dim Tot_images As New Int64
    Dim DR As SqlDataReader
    Dim con As New SqlConnection("data source= (local); initial catalog= hbet; user id=sa; password=; multipleactiveresultsets=true")
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As New SqlConnection("data source= (local); initial catalog= hbet; user id=sa; password=; multipleactiveresultsets=true")
        Try
            OFD1.Filter = "Jpg Files(*.Jpg)|*.Jpg" & "|Jpeg Files(*.Jpeg)|*.Jpeg" & "|Gif Files(*.Gif)|*.Gif"
            If OFD1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Label4.Text = OFD1.FileName
            Else
                Label4.Text = ""
                Label4.Visible = True
            End If
            If Label4.Text = "" Then
                PictureBox3.Visible = True
                'fun_imageload()
            Else
                lbl_path.Text = Label4.Text
                Dim imgdata() As Byte = readfile(lbl_path.Text)
                Dim ms As MemoryStream = New MemoryStream(imgdata, 0, imgdata.Length)
                ms.Write(imgdata, 0, imgdata.Length)
                Dim img As Image = Image.FromStream(ms, True)
                PictureBox3.Image = img
            End If
            'PictureBox1.Image = Image.FromFile()

            lbl_path.Text = Label4.Text
            Label4.Text = Label4.Text.Remove(Label4.Text.LastIndexOf("\"))


        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub
    Public Function readfile(ByVal FPath As String) As Byte()
        Dim data() As Byte
        Try
            Dim FI As New FileInfo(FPath)
            Dim numbytes As Long = FI.Length
            Dim fstream As FileStream = New FileStream(FPath, FileMode.Open, FileAccess.Read)
            Dim br As BinaryReader = New BinaryReader(fstream)
            data = br.ReadBytes(Convert.ToInt64(numbytes))
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
        Return data
    End Function


    Private Sub hbet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If con.State <> ConnectionState.Open Then
            con.Open()

        End If

        'Dim cmd As New SqlCommand(" select distinct studentid from hbet.dbo.studentmaster", con)
        'Dim dr As SqlDataReader
        'dr = cmd.ExecuteReader
        'While dr.Read
        '    ComboBox1.Items.Add(dr(0)).ToString()
        'End While
        'dr.Close()
        'Dim cmd As New SqlCommand

        Dim dr As SqlDataReader

        'cmd.Connection = con

        Dim cmd As New SqlCommand("select studentid + ' '+ studentname from hbet.dbo.studentmaster", con)

        ' con.Open()

        Try

            dr = cmd.ExecuteReader

            ComboBox1.Text = "---Select---"

            While dr.Read

                ComboBox1.Items.Add(dr(0))

            End While
            dr.Close()

        Catch ex As Exception

            MsgBox(ex.Message.ToString)
        End Try

    End Sub
    Dim file_count_no As Integer
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cmd As New SqlConnection("data source=(local);initial catalog=hbet;user id=sa; password=;multipleactiveresultsets=true")
        Try

            If ComboBox2.Text = "" And TextBox1.Text = "" And ComboBox2.Text = "" Then
                MsgBox("Fill details")
            Else

                If con.State <> ConnectionState.Open Then

                    con.Open()
                End If
                'If con.State = ConnectionState.Open Then
                '    con.Close()
                'Else
                '    con.Open()


                Dim files() As String
                files = Directory.GetFiles(Label4.Text)
                If files.Length > 0 Then
                    If files.Length > file_count_no Then
                        'End If
                        'files = Directory.GetFiles(Label4.Text)
                        Dim fn2 As String
                        'If files.Length > 0 Then
                        'files.Length > file_count_no 

                        fn2 = files.GetValue(file_count_no)
                        Dim imgdata() As Byte = readfile(fn2)
                    Else
                        MsgBox("All the pages in the folder are scan ned.")
                    End If
                Else
                    MsgBox("No Files are present.")
                End If
                If files.Length > 0 Then
                    Dim imgdata() As Byte = readfile(lbl_path.Text)
                    If con.State = ConnectionState.Closed Then
                        con.Open()
                    End If
                    Dim cmd1 As New SqlCommand("insert into HBET.dbo.HBETSCAN(studentid,name,doctype,image) values (@studentid, @name,@doctype,@Docimage)", con)
                    cmd1.Parameters.Add(New SqlParameter("@studentid", ComboBox1.Text.Substring(0, 8)))
                    cmd1.Parameters.Add(New SqlParameter("@Docimage", CType(imgdata, Object)))
                    cmd1.Parameters.Add(New SqlParameter("@name", TextBox1.Text))
                    cmd1.Parameters.Add(New SqlParameter("@doctype", ComboBox2.Text))
                    cmd1.CommandTimeout = 0
                    cmd1.ExecuteNonQuery()


                    MsgBox("Record Saved")

                    ComboBox1.Text = ""
                    ComboBox2.Text = ""
                    TextBox1.Text = ""


                    Dim files1() As String
                    files1 = Directory.GetFiles(Label4.Text)
                    Dim xyz As String
                    For Each xyz In files1
                        If xyz <> lbl_path.Text Then
                            Dim imgdata1() As Byte = readfile(xyz)
                            Dim ms As MemoryStream = New MemoryStream(imgdata1, 0, imgdata1.Length)
                            ms.Write(imgdata1, 0, imgdata1.Length)
                            Dim img As Image = Image.FromStream(ms, True)
                            PictureBox3.Image = img
                            File.Delete(lbl_path.Text)
                            lbl_path.Text = xyz
                            Exit For
                        ElseIf files.Length = 1 Then
                            Dim imgdata1() As Byte = readfile(xyz)
                            Dim ms As MemoryStream = New MemoryStream(imgdata1, 0, imgdata1.Length)
                            ms.Write(imgdata1, 0, imgdata1.Length)
                            Dim img As Image = Image.FromStream(ms, True)
                            PictureBox3.Image = img
                            File.Delete(xyz)
                            'lbl_path.Text = xyz
                            PictureBox3.Image = Nothing
                            Exit For
                        End If
                    Next
                    'MsgBox("Document Uploaded")
                    Dim files2() As String
                    files2 = Directory.GetFiles(Label4.Text)
                    If files2.Length = 0 Then
                        MsgBox("All the files are scanned.")
                        Label4.Text = ""
                        lbl_path.Text = """"
                    End If
                End If
            End If

        Catch ex As Exception

            MsgBox(ex.ToString())
        End Try
    End Sub
End Class
Posted
Updated 8-Mar-12 18:25pm
v3
Comments
Ganesan Senthilvel 9-Mar-12 0:25am
   
unlock your question to highlight the code
Varun Sareen 9-Mar-12 0:31am
   
I think you have to close the connection each time you open it and dispose of the connection object. Your problem will be solved :)

What you can do is trying to acquire an exclusive share on the file, if you success it means that the file is not used by any other process.

Use the FileShare enumerator when you open the file, therefore your steps should be:

1) Try to open a file with None sharing

2) Close the handle

3) If there is no exception you can work on the file.

Hope this helps.
   
I think the problem is in the method readfile. I would do a fstream.Dispose() within that method. Hopefully that fixes your problem.
   
Comments
Maciej Los 9-Mar-12 11:19am
   
I vote for this solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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