Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written the following code to insert data from multiple excel files sheet wise. But I get this error " '' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long." Please help.
C#
DataTable xls = new DataTable();
OleDbConnection SQLConn = new OleDbConnection(strConnectionString);

SQLConn.Open();

OleDbDataAdapter SQLAdapter = new OleDbDataAdapter();
string sql = "SELECT * FROM [" + sheetName + "]";
OleDbCommand selectCMD = new OleDbCommand(sql, SQLConn);

SQLAdapter.SelectCommand = selectCMD;

SQLAdapter.Fill(xls);

SQLConn.Close();
Posted
Updated 4-Nov-15 2:28am
v2
Comments
ZurdoDev 4-Nov-15 8:18am    
Sounds like sheetName is empty. And which line of code throws this error?
Richard MacCutchan 4-Nov-15 9:01am    
sheetName needs to contain the name of the worksheet that you want to read.
Member 12081613 4-Nov-15 11:03am    
when I debug I see the whole path to the sheet...isnt that correct? The fill line throws the exception
Member 12081613 4-Nov-15 11:14am    
Now I am getting this error once I put in the file name "The Microsoft Office Access database engine could not find the object" What does it mean?
[no name] 4-Nov-15 12:50pm    
Update below line and try, assuming sheetName variable is not null/empty -

string sql = "SELECT * FROM [" + sheetName + "$]";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900