“is not a valid name” error while retrieving data from csv file using oledb

is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.”

I encountered this error about two days back when I was retrieving data from a file with long name, I was not sure what was the cause of the problem so posted this as a question on stackoverflow. As the response was low, I tried to search the solution on my own but I didn’t exactly found what the problem was but it was clear that long file name was creating problem so here is what I did

ConnString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " & System.IO.Path.GetDirectoryName(strFileName) & "; Extended Properties = ""Text;HDR=YES;FMT=Delimited"""

'==========OleDB Supports fixed length file name.===========
Dim OldFileName As String = System.IO.Path.GetFileName(strFileName)
Dim NewFileName As String = Guid.NewGuid().ToString().Substring(0, 10) + System.IO.Path.GetExtension(OldFileName)
Dim rootPath As String = System.IO.Path.GetDirectoryName(strFileName) + "/"

'Rename file name
My.Computer.FileSystem.RenameFile(rootPath + OldFileName, NewFileName)
strFileName = rootPath + NewFileName

Dim strQuery As String
strQuery = "SELECT * FROM [" & System.IO.Path.GetFileName(strFileName) & "]"

'Revert rename file name
My.Computer.FileSystem.RenameFile(rootPath + NewFileName, OldFileName)
strFileName = rootPath + OldFileName

I just renamed the file with a new Guid while I am retrieving the data after renamed it back to original file name. This is not exactly what I wanted. Please comment if a better solution is there.


About Utkarsh Puranik

Software Engineer by Profession, Gamer by Nature, Techy by Attitude and a Good Person at Heart
This entry was posted in Technology and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s