Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am reading the excel using Oledb. I am getting object reference not set to an instance error when the name of excel file has ; (semicolon).

Please help me..

My code

C#
string OledbCon = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + FileName.Trim() + ";" + "Extended Properties=Excel 12.0;";

               OleCon = new OleDbConnection(OledbCon);



Fiel Name : somefiley;.xlsx.


Thanks,
prasad
Posted
Comments
Gopi Kishan Mariyala 12-Mar-15 8:45am    
Can you try regex to remove the semicolon

1. The semi colon as you know breaks the fields apart in a connection string. You can try putting square brackets [] around your file name but the best option is to remove the semi-colon from the file name.

According to, https://www.connectionstrings.com/formating-rules-for-connection-strings/[^] you can try delimiting it with ".
 
Share this answer
 
Hi,

I embedded the datasource in single quotes as below. It's worked for me.

C#
FileName = FileName.Replace("'", "''");
               string OledbCon = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='" + FileName.Trim() + "';" + "Extended Properties=Excel 12.0;";
 
Share this answer
 

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