Why is this generating a message that file is not found

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pooker75
    New Member
    • Nov 2007
    • 11

    #1

    Why is this generating a message that file is not found

    This code works perfectly except when I start I get a message that file is not found. The file is there and everything reads perfectly. How can I get the file to read and not get the message that the file is not found? Do I need to use something other than Try? I tried taking the Try away and then I get errors and no file reads. Jo Ann

    [CODE=vbnet]Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load

    'load the item into the combobox
    Try

    vinStreamreader = New StreamReader("c ar.txt")
    DisplayRecord()
    Catch ex As Exception
    'File is not found.
    MessageBox.Show ("File does not exist.")
    End Try


    End Sub
    Private Sub ComboBox1_Selec tedIndexChanged (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles ComboBox1.Selec tedIndexChanged
    'Read the next record.
    DisplayRecord()
    End Sub

    Private Sub DisplayRecord()
    'Read and display the next record.
    Dim vinString As String
    Do While vinStreamreader .Peek <> -1
    vinString = vinStreamreader .ReadLine()
    Me.ComboBox1.Te xt = vinString
    Me.ComboBox1.It ems.Add(ComboBo x1.Text)

    Me.ListBox1.Ite ms.Add(vinStrea mreader.ReadLin e())
    Me.ListBox2.Ite ms.Add(vinStrea mreader.ReadLin e())
    Me.ListBox3.Ite ms.Add(vinStrea mreader.ReadLin e())

    Loop
    'Display labels
    Me.Label4.Text = Me.ListBox1.Ite ms(Me.ComboBox1 .SelectedIndex)
    Me.Label5.Text = Me.ListBox2.Ite ms(Me.ComboBox1 .SelectedIndex)
    Me.Label6.Text = Me.ListBox3.Ite ms(Me.ComboBox1 .SelectedIndex)

    End Sub


    End Class[/CODE]
    Last edited by Killer42; Nov 26 '07, 09:11 AM.
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    If the code produces an error, then what do you mean by "working perfectly"?

    Presumably it's line 6 that produces the error? If so, then I guess you're looking for the file in the wrong directory. Try specifying the full path to the file.

    Comment

    • QVeen72
      Recognized Expert Top Contributor
      • Oct 2006
      • 1445

      #3
      Hi,

      You have hard-coded the message.
      Catch the actual Exception message. Change your code this way, and check ..

      [code=vbnet]
      Try
      vinStreamreader = New StreamReader("c ar.txt")
      DisplayRecord()
      Catch ex As Exception
      MessageBox.Show (ex.Message)
      End Try
      [/code]


      Regards
      Veena

      Comment

      • pooker75
        New Member
        • Nov 2007
        • 11

        #4
        I did what you said but it just generates another error message that I don't know how to fix. Please advise if you have any ideas how to fix so I don't get an error message.

        Comment

        • pooker75
          New Member
          • Nov 2007
          • 11

          #5
          Originally posted by Killer42
          If the code produces an error, then what do you mean by "working perfectly"?

          Presumably it's line 6 that produces the error? If so, then I guess you're looking for the file in the wrong directory. Try specifying the full path to the file.
          If I click ok on the message, the program proceeds to run just fine. I just need to get rid of the first error message that pops. The file is there and does not need the full file location to read. I just need the code to get rid of that error message. I tried changing file not found to ex.message but that just generates another error message that I can't fix.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Originally posted by pooker75
            ... I tried changing file not found to ex.message but that just generates another error message that I can't fix.
            I think we need to know the details of the error that is generated.

            Comment

            • pooker75
              New Member
              • Nov 2007
              • 11

              #7
              Originally posted by Killer42
              I think we need to know the details of the error that is generated.

              It says "Value cannot be null; Parameter Name: Item
              Last edited by pooker75; Nov 26 '07, 01:53 PM. Reason: adding info

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by pooker75
                It says "Value cannot be null; Parameter Name: Item
                Exactly where is it reporting this error? On the New StreamReader? If so, check out the Item parameter, since it apparently needs a value.

                Comment

                Working...