I am new to VB, so I appreciate this might seem like a stupid question.
I am using the following code to select a CSV file from the local file system.
Dim openFileDialog1 As New OpenFileDialog
openFileDialog1.InitialDirectory = "c:\uploads"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
'--TextField Parser is used to read the files
Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)
How can I make this error gracefully, if a file is not selected.
So in essence display a message saying "No file selected"
Thank you in advance.
Dim openFileDialog1 As New OpenFileDialog
Dim strFileName As String
openFileDialog1.InitialDirectory = "c:\uploads"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
strFiilename = openFileDialog1.FileName
If strFileName <> "" Then
'file is selected
Dim parser As New FileIO.TextFieldParser(strFiilename)
Else
MsgBox("No File Selected")
End If
End If
i think no need to check for this strFileName
because in your question you have placed
Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)
after
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
endif
this will leads your error