Format of the initialization string does not conform to specification starting at index 128

9.1k Views Asked by At

Mine is C# windows application. When i run this application in my local machine it gives following error:- "Format of the initialization string does not conform to specification starting at index 128".

try
{
    string path = System.IO.Path.GetFullPath("E:\\09-2013\\SalesRep\\Openleads.xlsx");
    if (Path.GetExtension(path) == ".xls")
    {
        oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; 
        Data Source=" + path + "Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
    }
    else if (Path.GetExtension(path) == ".xlsx")
    {
        oledbConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; 
        Data Source=" + path + "Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';");
    }

    oledbConn.Open();
    OleDbCommand cmd = new OleDbCommand(); ;
    OleDbDataAdapter oleda = new OleDbDataAdapter();
    DataSet ds = new DataSet();

    cmd.Connection = oledbConn;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT * FROM [Raw Data$]";
    oleda = new OleDbDataAdapter(cmd);
    oleda.Fill(ds, "dsSlno");
    grvData.DataSource = ds.Tables["dsSlno"].DefaultView;
    oleda = new OleDbDataAdapter(cmd);
    oleda.Fill(ds);

    grvData.DataSource = ds.Tables[1].DefaultView;
}
catch (Exception ex)
{

}
finally
{
    oledbConn.Close();
}
1

There are 1 best solutions below

0
On BEST ANSWER

Your path needs an @ sign in the front of it to escape your backslashes:

There is a null-terminating character (\0) after the 'E:' in it causing your issue when it's concatenated to your connection string.

Change this:

string path = System.IO.Path.GetFullPath("E:\09-2013\SalesRep\Openleads.xlsx");

To this:

string path = System.IO.Path.GetFullPath(@"E:\09-2013\SalesRep\Openleads.xlsx");

And you should be fine.