What's new

When user select file in OpenFileDialog Then ConnectionString gives error at Connection.Open()


Active member
Staff member
I am linking an excel file to transfer its data to ms-access database in C# application. I want the user to select the Excel file from OpenFileDialog and its path should be automatically go in ConnectionString. But its giving error at connExcel.Open(). OleDbException occurs invalid arguments.

ConnectionString code is under.

class DatabaseObjects
public static string filename = string.Empty;
public static string ConnectionStringExcel = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='"+filename+"';Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";

Code where user selecting the File is under.

private void btnImport_Click(object sender, EventArgs e)
OpenFileDialog openFileExcel = new OpenFileDialog();
openFileExcel.Filter = "Excel Files | *.xlsx; *.xls; *.xlsm";
openFileExcel.Title = "Select an Excel File";
if (openFileExcel.ShowDialog() == DialogResult.Cancel || openFileExcel.FileName.Equals(""))
DatabaseObjects.filename = openFileExcel.FileName;
using(OleDbConnection connExcel = new OleDbConnection(DatabaseObjects.ConnectionStringExcel))
string queryExcel = "SELECT * FROM [Six$]";
using (OleDbCommand commandExcel = new OleDbCommand(queryExcel,connExcel))

Continue reading...