Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing to text files Memory Issue

Status
Not open for further replies.

bradoirs

Technical User
Jul 8, 2009
35
GB
I am trying to generate a small txt file for each record in a table but I am getting out of memory errors.

This is all incredibly me so any help greatly appreciated and keep it simple please.
Code:
osql = "SELECT * FROM propertylist where fap<>0 order by thePrice desc"
oda = New OleDb.OleDbDataAdapter(osql, con)
oda.Fill(ds, "office")
oda.FillSchema(ds, SchemaType.Source, "office")
offmatchmax = ds.Tables("office").Rows.Count
For countoffice = 0 To offmatchmax - 1
   Dim ob As New OleDb.OleDbCommandBuilder(pda)     
   Form1.narrativeText.Visible = False
   Dim FILE_NAME As String = "c:\Fap\p1" & Trim(ds.Tables("office").Rows(countoffice).Item("theref")) & ".txt"
   Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
   Dim fulltxt As String
fulltxt = "<i>" & Trim(ds.Tables("office").Rows(countoffice).Item("asciiFull")) & "</i>"
   objWriter.WriteLine("<Font face=" & """" & "Times New Roman" & """" & ">" & Trim(ds.Tables("office").Rows(countoffice).Item("thename")) & "<p>" & Trim(fulltxt) & "<p>We are currently updating our listings so please contact us for full details<p>view this and other properties in a range of destinations at [URL unfurl="true"]www.suncastle.co.uk<p>Follow[/URL] us on twitter @suncastlePlus" & "</font>")
objWriter.Close()
I am obviously doing something wrong as the app grinds to a halt after appx 290 records- any suggestions?
 
I think it's because you are creating another StreamWriter object with each pass of the loop. DIM the StreamWriter outside the loop and use that one object over:

Code:
osql = "SELECT * FROM propertylist where fap<>0 order by thePrice desc"
oda = New OleDb.OleDbDataAdapter(osql, con)
oda.Fill(ds, "office")
oda.FillSchema(ds, SchemaType.Source, "office")
offmatchmax = ds.Tables("office").Rows.Count

[red]Dim objWriter As System.IO.StreamWriter[/red]

For countoffice = 0 To offmatchmax - 1
   Dim ob As New OleDb.OleDbCommandBuilder(pda)     
   Form1.narrativeText.Visible = False
   Dim FILE_NAME As String = "c:\Fap\p1" & Trim(ds.Tables("office").Rows(countoffice).Item("theref")) & ".txt"
   
   Dim fulltxt As String
fulltxt = "<i>" & Trim(ds.Tables("office").Rows(countoffice).Item("asciiFull")) & "</i>"

   [red]objWriter = New System.IO.StreamWriter(FILE_NAME)[/red]

   objWriter.WriteLine("<Font face=" & """" & "Times New Roman" & """" & ">" & Trim(ds.Tables("office").Rows(countoffice).Item("thename")) & "<p>" & Trim(fulltxt) & "<p>We are currently updating our listings so please contact us for full details<p>view this and other properties in a range of destinations at [URL unfurl="true"]www.suncastle.co.uk<p>Follow[/URL] us on twitter @suncastlePlus" & "</font>")
objWriter.Close()

In fact, I don't see any reason to DIM any of those variables in the loop. Move the DIM statements outside the loop, then clear/recreate the objects as needed.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top