Jevon Jevon - 1 month ago 18
C# Question

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

On this button click it should load the contents of an excel file into a dataGrid but instead am getting the error mentioned in the title of this post. What is it that am doing wrong?

private void button7_Click_2(object sender, EventArgs e)
{


string path = "C:\\Users\\jdavis\\Downloads\\Pharmacies\\CrossReferencing v3\\CrossReferencing\\\bin\\Debug\\cross_check.xls";
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + path + ";Extended Properties=' Excel 8.0;HDR=Yes;IMEX=1;';");
OleDbCommand command = new OleDbCommand
(
"SELECT"+"FROM [cross_check]",con

);
DataSet cross = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(cross);
dataGridView2.DataSource = cross.Tables[0];



}
}

Answer

Update the values as shown below, and this will get the ISAM and formatting issues fixed.

string path = " \"C:\\Users\\jdavis\\Downloads\\Pharmacies\\CrossReferencing v3\\CrossReferencing\\bin\\Debug\\cross_check.xls\" ";
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Ace.OLEDB.12.0;Data Source=" + path + ";Extended Properties= Excel 8.0;IMEX=1;");