Hi this is Ashwani and I am getting the above mentioned error while running the program, plz help me....
here's my code:-
//--------------------code-----------------------//
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
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
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 dr As SqlDataReader
Dim cmd As New SqlCommand("select studentid + ' '+ studentname from hbet.dbo.studentmaster", con)
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
Dim files() As String
files = Directory.GetFiles(Label4.Text)
If files.Length > 0 Then
If files.Length > file_count_no Then
Dim fn2 As String
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)
PictureBox3.Image = Nothing
Exit For
End If
Next
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