Hi Guys,
I have this code which updates SQL database using the files in a folder and delete them after update but its giving me an exception whenever the blank file is found in the folder. It gives error " The object reference not set to Instance of an object" . What i really want to do is before processing each file i need to check each file if its blank then delete it otherwise process it. this is the code :
Any help will be much appreciated.
Code:Dim dirinfo5 As DirectoryInfo Dim allFiles5() As FileInfo dirinfo5 = New DirectoryInfo("E:\UPDATE\") allFiles5 = dirinfo5.GetFiles("*.csv") Thread.Sleep(1000) If allFiles5.Length <> 0 Then Try For Each fl5 As FileInfo In allFiles5 'MsgBox(fl.FullName.ToString()) Dim con As SqlConnection = New SqlConnection(SQL_con2) Dim sr As StreamReader = New StreamReader(fl5.FullName) Dim line As String = sr.ReadLine Dim value() As String = line.Split(Microsoft.VisualBasic.ChrW(44)) Dim dt As DataTable = New DataTable Dim row As DataRow For Each dc As String In value dt.Columns.Add(New DataColumn(dc)) Next While Not sr.EndOfStream value = sr.ReadLine.Split(Microsoft.VisualBasic.ChrW(44)) If (value.Length = dt.Columns.Count) Then row = dt.NewRow row.ItemArray = value dt.Rows.Add(row) End If End While Dim bc As SqlBulkCopy = New SqlBulkCopy(con.ConnectionString, SqlBulkCopyOptions.TableLock) bc.DestinationTableName = "[DB].[dbo].[LData]" bc.BatchSize = dt.Rows.Count con.Open() bc.WriteToServer(dt) bc.Close() con.Close() sr.Close() System.IO.File.Delete(fl5.FullName) sr.Dispose() Next Catch ex As Exception MsgBox(ex.Message) End Try End If




Reply With Quote
