Results 1 to 5 of 5

Thread: Double execute

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Double execute

    I am having troubles writing to a CSV file, as VB.NET seems to think it is already in use by another app...

    Here is a quick cut down version of the code:

    VB Code:
    1. Dim di As New FileInfo("users.csv")
    2. If di.Length <> 0 Then
    3.   Dim userFile As New IO.StreamWriter("users.csv", True)
    4.   userFile.Write("here is some text, and here is more")
    5.   userFile.Close()
    6. End If

    If required, I can post the entire chunk, but from what you see above, is there any reason why VB.NET will not write to users.csv?

    Cheers.

  2. #2
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Double execute

    Hey...
    I tried..
    Code:
      Dim di As New System.IO.FileInfo(Application.StartupPath & "\users.csv")
            If di.Length = 0 Then
                Dim userFile As New IO.StreamWriter(Application.StartupPath & "\users.csv",true)
                userFile.Write("here is some text, and here is more")
                userFile.Close()
            End If
    But,It works fine for me this way..the text gets added..I find no problem..I just created a file..users.csv..thats all I wonder what problem its giving u...

  3. #3
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Double execute

    Hey..
    Maybe you didnt close the stream somewhere else in the program

    Godwin

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Double execute

    OK here is a more detailed chunk:

    VB Code:
    1. Dim fileResume As Boolean
    2.                 If System.IO.File.Exists("users.csv") Then
    3.                     Dim di As New FileInfo("users.csv")
    4.                     If di.Length <> 0 Then
    5.                         ' Resume File
    6.                         fileResume = True
    7.                         Dim Form2 As New Form2
    8.                         'Dim csvFile As String
    9.                         'Dim csvBank As String
    10.                         If Form2.ShowDialog() = DialogResult.OK Then
    11.                             csvFile = Form2.listItem
    12.                             csvBank = Form2.newBank
    13.                             frm3File = Form2.Frm3File
    14.                             frm3User = Form2.Frm3User
    15.                         End If
    16.                         If csvBank = "New Project" Then
    17.                             fileResume = False
    18.                             MessageBox.Show(csvFile, "filename")
    19.                             Dim userFile As New IO.StreamWriter("users.csv", True)
    20.                             Dim m_today As Date
    21.                             m_today = Date.Today
    22.                             userFile.Write(m_today & "," & frm3User & "," & frm3File & ".csv")
    23.                             userFile.Close()
    24.                             csvFile = frm3File
    25.                         Else
    26.                             fileResume = True
    27.                             Dim commaseparator As Char() = ","
    28.                             Dim srd As StreamReader = Nothing
    29.                             Try
    30.                                 srd = New StreamReader(csvFile)
    31.                                 line = srd.ReadLine()
    32.                                 While Not (line Is Nothing)
    33.                                     fields = line.Split(commaseparator)
    34.                                     If fields.Length <> 3 Then
    35.                                         GoTo ContinueWhile4
    36.                                     End If
    37.                                     Dim qe As QuestionElement
    38.                                     For Each qe In gt
    39.                                         If qe.Id = Int32.Parse(fields(1)) Then
    40.                                             qe.Answered = True
    41.                                         End If
    42.                                     Next qe
    43. ContinueWhile4:
    44.                                     line = srd.ReadLine()
    45.                                 End While
    46.                             Catch
    47.                             Finally
    48.                                 If Not (Nothing Is srd) Then
    49.                                     CType(srd, IDisposable).Dispose()
    50.                                 End If
    51.                             End Try
    52.                         End If
    53.                     End If
    54.                 Else
    55.                     'Dim m_fileUsers As New IO.StreamWriter("users.csv")
    56.                     Dim m_today As Date
    57.                     m_today = Date.Today
    58.                     Dim myForm3 As New Form3
    59.                     If myForm3.ShowDialog() = DialogResult.OK Then
    60.                         csvFile = myForm3.MyFilename
    61.                     End If
    62.                     csvFile = csvFile & ".csv"
    63.                     Dim m_fileUsers As New IO.StreamWriter("users.csv")
    64.                     m_fileUsers.Write(m_today & "," & myForm3.MyUsername & "," & myForm3.MyFilename & ".csv")
    65.                     m_fileUsers.Close()
    66.                 End If

    Basically, it won't let me write to users.csv

  5. #5
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    Re: Double execute

    Hey Cd...
    I wonder if uve declared csvfile,frm3file,frm3user as string or something else..
    And in the 1st part of the if statements,ur not using filestreams...
    To avoid all this errors,just use filestreams everywhere...open it while writing to it and then close it..
    In this case,I think the file is getting locked and isnt released

    Godwin
    P.S:I really dont understand what this code actually is...but,i think in the stuff in the beginning have no filestreams..

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