By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,594 Members | 2,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,594 IT Pros & Developers. It's quick & easy.

Why is this generating a message that file is not found

P: 11
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

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.  
  3.         'load the item into the combobox
  4.         Try
  5.  
  6.             vinStreamreader = New StreamReader("car.txt")
  7.             DisplayRecord()
  8.         Catch ex As Exception
  9.             'File is not found.
  10.             MessageBox.Show("File does not exist.")
  11.         End Try
  12.  
  13.  
  14.     End Sub
  15.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  16.         'Read the next record.
  17.         DisplayRecord()
  18.     End Sub
  19.  
  20.     Private Sub DisplayRecord()
  21.         'Read and display the next record.
  22.         Dim vinString As String
  23.         Do While vinStreamreader.Peek <> -1
  24.             vinString = vinStreamreader.ReadLine()
  25.             Me.ComboBox1.Text = vinString
  26.             Me.ComboBox1.Items.Add(ComboBox1.Text)
  27.  
  28.             Me.ListBox1.Items.Add(vinStreamreader.ReadLine())
  29.             Me.ListBox2.Items.Add(vinStreamreader.ReadLine())
  30.             Me.ListBox3.Items.Add(vinStreamreader.ReadLine())
  31.  
  32.         Loop
  33.         'Display labels
  34.         Me.Label4.Text = Me.ListBox1.Items(Me.ComboBox1.SelectedIndex)
  35.         Me.Label5.Text = Me.ListBox2.Items(Me.ComboBox1.SelectedIndex)
  36.         Me.Label6.Text = Me.ListBox3.Items(Me.ComboBox1.SelectedIndex)
  37.  
  38.     End Sub
  39.  
  40.  
  41. End Class
Nov 26 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 5K+
P: 8,434
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.
Nov 26 '07 #2

QVeen72
Expert 100+
P: 1,445
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. Try 
  2.     vinStreamreader = New StreamReader("car.txt")
  3.     DisplayRecord()
  4. Catch ex As Exception
  5.      MessageBox.Show(ex.Message)
  6. End Try
  7.  

Regards
Veena
Nov 26 '07 #3

P: 11
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.
Nov 26 '07 #4

P: 11
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.
Nov 26 '07 #5

Expert 5K+
P: 8,434
... 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.
Nov 26 '07 #6

P: 11
I think we need to know the details of the error that is generated.

It says "Value cannot be null; Parameter Name: Item
Nov 26 '07 #7

Expert 5K+
P: 8,434
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.
Nov 27 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.