Results 1 to 5 of 5

Thread: Streamreader close error

  1. #1

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Streamreader close error

    Hi all,
    I am using a streamreader to read txt files. This all works fine, but once it's done, I want to copy the text file to another location. In order to do this, I should close the streamreader, in order for the system to release the txt file right?

    Well, this seems not to be working. I call streamreader.close.... but it doesn't release my txt file...

    This is some of my code:


    VB Code:
    1. Select Case whatToDo
    2.     Case "V"
    3.         theFile.Delete()
    4.     Case "B"
    5.         sr.Close()
    6.         IO.File.Move("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name, "C:\Netcash\DebitProcessor\3_MasterBatch\" & theFile.Name)
    7.     Case "C"
    8.         sr.Close()
    9.         IO.File.Move("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name, "C:\Netcash\DebitProcessor\3_MasterBatch\" & theFile.Name)
    10. End Select


    and this is my error:




    An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

    Additional information: The process cannot access the file "C:\3_MasterBatch\19642632673021796427000.txt" because it is being used by another process.

    Thanks

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Streamreader close error

    Could be the system hasn't had time to finish writing the file.

    After your close, you could add a line:

    GC.Collect()

    And see if it helps. This will manually force the Garbage collection to free up resources in use. I have had it help in the past with access issues.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Streamreader close error

    Hey there,

    I called gc.collect... but I still have the same problem.

    What else?

    This is my code thusfar:

    VB Code:
    1. Dim SRead As New IO.StreamReader("C:\DebitProcessor\2_Verification\" & theFile.Name)
    2.                     'OFile.OpenText("C:\DebitProcessor\2_Verification\" & theFile.Name)
    3.                     ''SR = New IO.StreamReader("C:\DebitProcessor\2_Verification\" & theFile.Name)
    4.  
    5.                     strLine = SRead.ReadLine()
    6.                     strLine = SRead.ReadLine()
    7.                     strLine = SRead.ReadLine()
    8.                     theProgressCount = 2
    9.                     Do While Not strLine Is Nothing
    10.  
    11.                         LineArray = Split(strLine, """,""", -1)
    12.                         theProgressCount = theProgressCount + 1
    13.                         For i = 0 To UBound(LineArray)
    14.                             theFieldValue = Replace(LineArray(i), """", "")
    15. SRead.Close()
    16.                     GC.Collect()
    17.                     If ErrorDetected = True Then
    18.                         theErrorString = theErrorString & "Please check your documentation to see the explanation for the error codes." & vbCrLf & vbCrLf & "Thank You" & vbCrLf & ""
    19.  
    20.                         Mailer.SendMail(ReplyMail, ReplyMail, "103 CDV Check return from Netcash. ", "Your data has failed the CDV check. Below you will see the records that have failed." & vbCrLf & vbCrLf & theErrorString)
    21.                         Mailer.SendMail(SupportMail, SupportMail, "103 CDV Check return from Netcash. ", "Your data has failed the CDV check. Below you will see the records that have failed." & vbCrLf & vbCrLf & theErrorString)
    22.                         'Select Case Err.Number
    23.                         '   Case 0                             'no error
    24.                         '       'MsgBox("allok")
    25.                         '   Case Else
    26.                         '       MsgBox("An error occurred: " + Err.Description + " " + Err.Number + " " + Err.HelpContext)
    27.                         '       End
    28.                         'End Select
    29.  
    30.                     Else
    31.                         Mailer.SendMail(ReplyMail, ReplyMail, "102 CDV Check Complete. Updates and/or Batch queued to follow. ", "Your File has run through the CDV successfully. Please remember that if you have sent us a batch instruction, that it will need to be authorized online. " & vbCrLf & vbCrLf & "Thank You" & vbCrLf & "Netcash")
    32.                         'Mailer.SendMail(SupportMail, SupportMail, "102 CDV Check Complete. Updates and/or Batch queued to follow. ", "Your File has run through the CDV successfully. Please remember that if you have sent us a batch instruction, that it will need to be authorized online. " & vbCrLf & vbCrLf & "Thank You" & vbCrLf & "")
    33.                     End If
    34.  
    35.                     SRead.Close()
    36.  
    37.                     Select Case whatToDo
    38.                         Case "V"
    39.                             theFile.Delete()
    40.                         Case "B"
    41.                             IO.File.Move("C:\DebitProcessor\2_Verification\" & theFile.Name, "C:\DebitProcessor\3_MasterBatch\" & theFile.Name)
    42.                         Case "C"
    43.                             IO.File.Move("C:\DebitProcessor\2_Verification\" & theFile.Name, "C:\DebitProcessor\3_MasterBatch\" & theFile.Name)
    44.                     End Select

    Thanks

  4. #4
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Streamreader close error

    Where is your "Loop" statement? Can you add the code until it hits that point?

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Addicted Member Tjoppie's Avatar
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    241

    Re: Streamreader close error

    Hi,

    Ok, this is all the code:

    VB Code:
    1. Dim SRead As New IO.StreamReader("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name)
    2.  
    3. strLine = SRead.ReadLine()
    4. strLine = SRead.ReadLine()
    5. strLine = SRead.ReadLine()
    6. theProgressCount = 2
    7. Do While Not strLine Is Nothing
    8. LineArray = Split(strLine, """,""", -1)
    9. theProgressCount + 1
    10. For i = 0 To UBound(LineArray)
    11. theFieldValue = Replace(LineArray(i), """", "")
    12. Select Case i
    13.                                 Case 0
    14. If theFieldValue = "##END##" Then
    15.  
    16.     End If
    17.                                     theAccountReferrence = Validation.FReturnString(theFieldValue, 22)
    18.                                 Case 1
    19.                                     theAccountName = Validation.FReturnString(theFieldValue, 49)
    20.                                 Case 2
    21.                                     theBankAccountName = Validation.FReturnString(theFieldValue, 49)
    22.                                 Case 3
    23.                                     theAccountType = Validation.FReturnNumber(theFieldValue, 1)
    24.                                 Case 4
    25.                                     theBranchCode = Validation.FillBranchCode(theFieldValue)
    26.                                 Case 5
    27.                                     theBankAccountNumber = Validation.FReturnNumber(theFieldValue, 16)
    28.                                 Case 6
    29.                                     theContractAmount = theFieldValue / 100
    30.                                 Case 7
    31.                                     theBatchAmount = theFieldValue / 100
    32.                                 Case 8
    33.                                     If Validation.FEmailVerify(theFieldValue) = False Then
    34.                                         theEmail = ""
    35.                                     Else
    36.                                         theEmail = theFieldValue
    37.                                     End If
    38.                                 Case 9
    39.                                     theNameOnCC = Validation.FReturnString(theFieldValue, 49)
    40.                                 Case 10
    41.                                     theNumberOnCC = Validation.FReturnNumber(theFieldValue, 16)
    42.                                 Case 11
    43.                                     theMonthExpires = Validation.FReturnNumber(theFieldValue, 2)
    44.                                 Case 12
    45.                                     theYearExpires = Validation.FReturnNumber(theFieldValue, 4)
    46.                                 Case 13
    47.                                     theCardType = Validation.FReturnNumber(theFieldValue, 1)
    48.                                 Case 14
    49.                                     IsCreditCard = Validation.FReturnNumber(theFieldValue, 1)
    50.                                 Case 15
    51.                                     theExtra1 = theFieldValue
    52.                                 Case 16
    53.                                     theExtra2 = theFieldValue
    54.                                 Case 17
    55.                                     theExtra3 = theFieldValue
    56.                             End Select
    57.                         Next
    58.                         If IsCreditCard = 0 Then
    59.                             VReturn = 0
    60.                             VReturn = Validation.CDVValidateAccount(theBankAccountNumber, theBranchCode, theAccountType)
    61.                             If VReturn <> 0 Then
    62.                                 ErrorDetected = True
    63.                                 Select Case VReturn
    64.                                     Case 0
    65.                                         theErrorText = "OK"
    66.                                     Case 1
    67.                                         theErrorText = "INVALID BRANCH CODE"
    68.                                     Case 2
    69.                                         theErrorText = "INVALID ACCOUNT NO"
    70.                                     Case 3
    71.                                         theErrorText = "INVALID ACCOUNT TYPE"
    72.                                 End Select
    73.                                 'MsgBox(theAccountReferrence & " : " & theAccountType & " : " & theBranchCode & " : " & theBankAccountNumber)
    74.                                 theErrorString = theErrorString & "Acc Ref :" & theAccountReferrence & " Line : " & theProgressCount & " has a CDV Error No : " & VReturn & " - " & theErrorText & vbCrLf
    75.                             End If
    76.                             VReturn = 0
    77.                         Else
    78.                             Select Case theCardType
    79.                                 Case "1"
    80.                                     theStrCardType = "MasterCard"
    81.                                 Case "2"
    82.                                     theStrCardType = "Visa"
    83.                                 Case "3"
    84.                                     theStrCardType = "AMEX"
    85.                                 Case "4"
    86.                                     theStrCardType = "Diners"
    87.                                 Case Else
    88.                                     theStrCardType = "0"
    89.                             End Select
    90.                             If Validation.FIsCreditCard(theStrCardType, theNumberOnCC) = False Then
    91.                                 ErrorDetected = True
    92.                                 theErrorString = theErrorString & "Line : " & theProgressCount & " has an Invalid Credit Card Number : " & vbCrLf
    93.                             End If
    94.                         End If
    95.                         strLine = SRead.ReadLine()
    96.                     Loop
    97.  
    98.                     SRead.Close()
    99.                     GC.Collect()
    100.  
    101.                     If ErrorDetected = True Then
    102.                         theErrorString = theErrorString & "Please check your documentation to see the explanation for the error codes." & vbCrLf & vbCrLf & "Thank You" & vbCrLf & "Netcash"
    103.  
    104.                         Mailer.SendMail(ReplyMail, ReplyMail, "103 CDV Check return from Netcash. ", "Your data has failed the CDV check. Below you will see the records that have failed." & vbCrLf & vbCrLf & theErrorString)
    105.                         Mailer.SendMail(NetcashSupportMail, NetcashSupportMail, "103 CDV Check return from Netcash. ", "Your data has failed the CDV check. Below you will see the records that have failed." & vbCrLf & vbCrLf & theErrorString)
    106.                         'Select Case Err.Number
    107.                         '   Case 0                             'no error
    108.                         '       'MsgBox("allok")
    109.                         '   Case Else
    110.                         '       MsgBox("An error occurred: " + Err.Description + " " + Err.Number + " " + Err.HelpContext)
    111.                         '       End
    112.                         'End Select
    113.  
    114.                     Else
    115.                         Mailer.SendMail(ReplyMail, ReplyMail, "102 CDV Check Complete. Updates and/or Batch queued to follow. ", "Your File has run through the CDV successfully. Please remember that if you have sent us a batch instruction, that it will need to be authorized online. " & vbCrLf & vbCrLf & "Thank You" & vbCrLf & "Netcash")
    116.                         'Mailer.SendMail(NetcashSupportMail, NetcashSupportMail, "102 CDV Check Complete. Updates and/or Batch queued to follow. ", "Your File has run through the CDV successfully. Please remember that if you have sent us a batch instruction, that it will need to be authorized online. " & vbCrLf & vbCrLf & "Thank You" & vbCrLf & "Netcash")
    117.                     End If
    118.  
    119.                     Select Case whatToDo
    120.                         Case "V"
    121.                             'theFile.Delete()
    122.                             OFile.Delete("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name)
    123.                         Case "B"
    124.                             OFile.Move("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name, "C:\Netcash\DebitProcessor\3_MasterBatch\" & theFile.Name)
    125.                             IO.File.Move("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name, "C:\Netcash\DebitProcessor\3_MasterBatch\" & theFile.Name)
    126.                         Case "C"
    127.                             IO.File.Move("C:\Netcash\DebitProcessor\2_Verification\" & theFile.Name, "C:\Netcash\DebitProcessor\3_MasterBatch\" & theFile.Name)
    128.                     End Select

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