469,590 Members | 2,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,590 developers. It's quick & easy.

Getting Error: ObjectDisposedException was unhandled...Safe handle has been closed

Hi everyone, first post here AND new to VB.NET .. So please excuse any goofs that follow!

I have written a simple console application that extracts data from multiple XML files and inserts the data into a table which resides on our AS/400. All of the code appears to be working correctly and all the data which I expect from the XML files does end up in the table on the 400 ... But when the project is finishing/closing, I get this error thrown at me: ObjectDisposedException was unhandled ... Safe handle has been closed

I am on .NET 2005 on an XP machine. Below is the code that I am using (Also, I am SURE that there are BETTER ways to go about doing what I'm doing ... But hey, I'm learning here!!!). If there is more information needed from me, just let me know ... And thanks in advance for any help on this.

Expand|Select|Wrap|Line Numbers
  1. Module Module1
  2.     Sub Main()
  3.         Dim obj400Conn As New _
  4.         iSeries.iDB2Connection("DataSource=SOURCE;DefaultCollection=COLLECTION;userid=ID;password=PASS;")
  5.  
  6.         Dim objAdapter As iSeries.iDB2DataAdapter
  7.         Dim objDataRowH, objDataRowD, objDBRow As DataRow
  8.         Dim objDSXML As New DataSet()
  9.         Dim objDSDBTable As New DataSet("F55FLFBP")
  10.         Dim ObjCmdBuilder As iSeries.iDB2CommandBuilder
  11.  
  12.         For Each File As IO.FileInfo In New System.IO.DirectoryInfo("\\XMLDIR\").GetFiles
  13.             objDSXML.Tables.Clear()
  14.             objDSXML.ReadXml("\\XMLDIR\" & File.Name)
  15.             obj400Conn.Open()
  16.  
  17.             objAdapter = New iSeries.iDB2DataAdapter("SELECT BP55FLTID, BPANI, BPTOTA, BP55FLSCAC, BP55FLVND#, BP55FLBOL, BP55FLFBN, BP55FLFBD, BPURCD FROM TSTDATA.F55FLFBP", obj400Conn)
  18.             objAdapter.Fill(objDSDBTable, "F55FLFBP")
  19.             Dim keyColumn(2) As DataColumn
  20.             keyColumn(0) = objDSDBTable.Tables(0).Columns("BP55FLTID")
  21.             keyColumn(1) = objDSDBTable.Tables(0).Columns("BPANI")
  22.             objDSDBTable.Tables(0).PrimaryKey = keyColumn
  23.  
  24.             For Each objDataRowH In objDSXML.Tables(0).Rows
  25.                 With objDSDBTable.Tables(0)
  26.                     For Each objDataRowD In objDSXML.Tables(1).Rows
  27.                         If objDataRowH(0) = objDataRowD(0) Then
  28.                             objDBRow = .NewRow()
  29.                             objDBRow(0) = objDataRowH(0)
  30.                             objDBRow(1) = objDataRowD(1)
  31.                             objDBRow(2) = objDataRowD(2) * 100
  32.                             objDBRow(3) = objDataRowH(1)
  33.                             objDBRow(4) = objDataRowH(2)
  34.                             objDBRow(5) = objDataRowH(3)
  35.                             objDBRow(6) = objDataRowH(4)
  36.                             objDBRow(7) = objDataRowH(5)
  37.                             objDBRow(8) = "N"
  38.                             .Rows.Add(objDBRow)
  39.                         End If
  40.                     Next
  41.                 End With
  42.             Next
  43.  
  44.             ObjCmdBuilder = New iSeries.iDB2CommandBuilder(objAdapter)
  45.             objAdapter.Update(objDSDBTable, "F55FLFBP")
  46.             obj400Conn.Close()
  47.  
  48.             'Move XML file from Processing DIR to Archive DIR
  49.             IO.File.Move("\\XMLDIR\" & File.Name, "\\XMLDIR\Archive\" & File.Name)
  50.  
  51.         Next
  52. End Sub
  53. End Module
  54.  
May 1 '07 #1
3 13048
Frinavale
9,735 Expert Mod 8TB
Hi everyone, first post here AND new to VB.NET .. So please excuse any goofs that follow!

I have written a simple console application that extracts data from multiple XML files and inserts the data into a table which resides on our AS/400. All of the code appears to be working correctly and all the data which I expect from the XML files does end up in the table on the 400 ... But when the project is finishing/closing, I get this error thrown at me: ObjectDisposedException was unhandled ... Safe handle has been closed

I am on .NET 2005 on an XP machine. Below is the code that I am using (Also, I am SURE that there are BETTER ways to go about doing what I'm doing ... But hey, I'm learning here!!!). If there is more information needed from me, just let me know ... And thanks in advance for any help on this.
...

If you recreate a resource many times the deconstructor for that object isn't called to dispose of any of the resources no longer being used or referenced. This is why we normally call the Dispose() function whenever we're done using a resource like a database connection.

If you create an object on the managed heap that allocates resources such as handlers or connections, then you must ensure that the object has released its resources before allowing the garbage collection to recycle the object.

It sounds like you are letting the garbage collector recycle objects that are used by other resources. It could be your database handers or even your file reference resources.

I'd look at your code a little more but I don't have time right now so I'll leave you with thinking about manually managing when objects are disposed of.

I recommend looking up the IDisposable interface.
Check out this link to find out why this is so important. It might also shed some light on your error.

Cheers!

-Frinny
May 1 '07 #2
Thanks much for the reply back Frinavale! I read through the article, trying to expand my .NET knowhow a bit more (I found most of it a bit of it over my head unfortunatley...I have been working with VB & .NET for only a couple weeks).

Anywho, I have gotten to the source of my error (Or so I think for now). I did not have the latest Service Pack (SI26879) installed for my IBM Client Access version (V5R4M0). Once I installed the service pack, the error was gone with no modification done to the code.

-Shad3JD
May 2 '07 #3
Frinavale
9,735 Expert Mod 8TB
Thanks much for the reply back Frinavale! I read through the article, trying to expand my .NET knowhow a bit more (I found most of it a bit of it over my head unfortunatley...I have been working with VB & .NET for only a couple weeks).

Anywho, I have gotten to the source of my error (Or so I think for now). I did not have the latest Service Pack (SI26879) installed for my IBM Client Access version (V5R4M0). Once I installed the service pack, the error was gone with no modification done to the code.

-Shad3JD

Congratz Shad3JD!

:)
May 3 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by amber | last post: by
11 posts views Thread by hazz | last post: by
5 posts views Thread by kimtherkelsen | last post: by
4 posts views Thread by Bernard Borsu | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.