Nepethya Rana

Nepethya Rana

  • NA
  • 335
  • 152.8k

ObjectDisposedException

May 14 2017 11:35 PM
Hi, 
 
I got randomly Object disposed exception in my application 1 out of 5 times if I ran the program. It does not throw exceptions every time i ran the program. 
 
stacktrace says the exception is occured at the line where file1 = nothing.
 
How do i solve this erratick behaviour ?
 
Better way to implemt the code?  
 
Dim sbSQL As New StringBuilder
sbSQL.Append("SELECT Item_Id from MyTableA")
Dim ds1 As New DataSet
Using daFindOutdated As New SqlDataAdapter(sbSQL.ToString, cn)
daFindOutdated.Fill(ds1)
End Using
Dim sbPath As New StringBuilder
For Each row As DataRow In ds1.Tables(0).Rows
sbPath.Append(ConfigurationManager.AppSettings("ZipFilePath") & "TBL" & row.Item("Item_ID").ToString.PadLeft(5, "0") & ".zip")
If File.Exists(sbPath.ToString) Then
Dim zip As New ZipArchive(New DiskFile(sbPath.ToString))
Dim folder1 As AbstractFolder = zip.GetFolder("\Item\File\")
Dim file1 As AbstractFile = folder1.GetFile("Tbl.cpf")
Using sw1 As New StreamWriter(file1.OpenWrite(True))
sw1.AutoFlush = True
sw1.WriteLine("")
sw1.Close()
file1 = Nothing
End Using
Dim folder2 As AbstractFolder = zip.GetFolder("\Item\File\")
Dim file2 As AbstractFile = folder2.GetFile("Tbl.cmp")
Using sw2 As New StreamWriter(file2.OpenWrite(True))
sw2.AutoFlush = True
sw2.WriteLine("")
sw2.Close()
file2 = Nothing
End Using
Dim folder3 As AbstractFolder = zip.GetFolder("\Item\File\")
Dim file3 As AbstractFile = folder3.GetFile("Tbl.upc")
Using sw3 As New StreamWriter(file3.OpenWrite(True))
sw3.AutoFlush = True
sw3.WriteLine("")
sw3.Close()
file3 = Nothing
End Using
zip = Nothing
End If
sbPath.Length = 0
Next
 

Answers (1)