Results 1 to 6 of 6

Thread: Str File writter

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    40

    Str File writter

    I have a question about my save data button this is what I have... the only thing is that it is clearing out all the records and not writting anything back am I missing something?

    fswOutput = New StreamWriter("LibraryInfo.txt", False)

    Dim intInfo As Integer

    Dim strRecord As String

    Dim strBookID As String

    Dim strAuthor As String

    Dim strTitle As String

    Dim strCost As String

    Dim strCopies As String

    Dim intCount As Integer



    'NOt working

    For intInfo = 0 To intCount - 1

    strRecord = "jc,"

    strRecord += Me.lstJCBookID.Items.Item(intInfo) + ","

    strRecord += Me.lstJCAuthor.Items.Item(intInfo) + ","

    strRecord += Me.lstJCTitle.Items.Item(intInfo) + ","

    strRecord += Me.lstJCCost.Items.Item(intInfo) + ","

    strRecord += Me.lstJCCopies.Items.Item(intInfo)

    fswOutput.WriteLine(strRecord)

    Next



    Thanks in advance....

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Str File writter

    try adding fswOutput.Close() at the end like so:

    VB Code:
    1. fswOutput = New StreamWriter("LibraryInfo.txt", False)
    2.  
    3. Dim intInfo As Integer
    4.  
    5. Dim strRecord As String
    6.  
    7. Dim strBookID As String
    8.  
    9. Dim strAuthor As String
    10.  
    11. Dim strTitle As String
    12.  
    13. Dim strCost As String
    14.  
    15. Dim strCopies As String
    16.  
    17. Dim intCount As Integer
    18.  
    19.  
    20.  
    21. 'NOt working
    22.  
    23. For intInfo = 0 To intCount - 1
    24.  
    25. strRecord = "jc,"
    26.  
    27. strRecord += Me.lstJCBookID.Items.Item(intInfo) + ","
    28.  
    29. strRecord += Me.lstJCAuthor.Items.Item(intInfo) + ","
    30.  
    31. strRecord += Me.lstJCTitle.Items.Item(intInfo) + ","
    32.  
    33. strRecord += Me.lstJCCost.Items.Item(intInfo) + ","
    34.  
    35. strRecord += Me.lstJCCopies.Items.Item(intInfo)
    36.  
    37. fswOutput.WriteLine(strRecord)
    38.  
    39. Next
    40. fswOutput.Close() 'add here

  3. #3
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: Str File writter

    Also don't use + to add to a string. Use &

    VB Code:
    1. strRecord += Me.lstJCCost.Items.Item(intInfo) + ","
    2.  
    3.         [b]becomes[/b]
    4.  
    5. strRecord += Me.lstJCCost.Items.Item(intInfo) & ","

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    40

    Re: Str File writter

    Thank you for responding, I tried that and it still cleared out the text file

  5. #5
    New Member
    Join Date
    Apr 2005
    Posts
    13

    Re: Str File writter

    The problem that I see is with this line of code

    VB Code:
    1. fswOutput = New StreamWriter("LibraryInfo.txt", False)

    Every time you trigger the event you're overwriting the file.

    Try using the following

    VB Code:
    1. Dim strFilename As String = "path\filename"
    2. Dim fswOutput As StreamWriter = New StreamWriter(strFilename, False)
    3.  
    4.  
    5. If File.Exists(strFilename) Then
    6.  
    7.        fswOutput = File.AppendText(strFilename)
    8.  
    9.    Else
    10.  
    11.        fswOutput = File.CreateText(strFilename)
    12.  
    13. End If
    14.  
    15. For intInfo = 0 To intCount - 1
    16.  
    17. strRecord = "jc,"
    18.  
    19. strRecord += Me.lstJCBookID.Items.Item(intInfo) + ","
    20.  
    21. strRecord += Me.lstJCAuthor.Items.Item(intInfo) + ","
    22.  
    23. strRecord += Me.lstJCTitle.Items.Item(intInfo) + ","
    24.  
    25. strRecord += Me.lstJCCost.Items.Item(intInfo) + ","
    26.  
    27. strRecord += Me.lstJCCopies.Items.Item(intInfo)
    28.  
    29. fswOutput.WriteLine(strRecord)
    30.  
    31. Next
    32.  
    33.  
    34.         fswOutput.Flush()
    35.         fswOutput.Close()


    Tar

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Str File writter

    just do this:

    Dim fswOutput As StreamWriter = New StreamWriter(strFilename) 'take out the false

    Now the code i showed earlier should work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width