3

i am trying to read html file through OLEDB reader using following code

DataTable dTable;
string strDataSource = "";
string strDBFile = "";
long intPos = 0;
strDataSource = mstrFilePath;
dTable = new DataTable();
mCon = new System.Data.OleDb.OleDbConnection();
mCon.ConnectionString  = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName + ";Extended Properties=\"HTML Import;HDR=NO;IMEX=1\";");     
if (mCon.State == ConnectionState.Closed)
{
    mCon.Open(); // gettting failed here
}
dTable = mCon.GetSchema("Tables");
        bSelectionChanged = true;
        lstTables.Items.Clear();
        foreach (DataRow DRow in dTable.Rows)
        {

            if (DRow["TABLE_TYPE"].ToString() == "TABLE" || DRow["TABLE_TYPE"].ToString() == "VIEW")
            {
                intPos = DRow["TABLE_NAME"].ToString().LastIndexOf("FilterDatabase");
                lstTables.Items.Add(DRow["TABLE_NAME"]);
            }
        }
        if (lstTables.Items.Count == 1)
        {
            lstTables.Visible = false;
            grdSampleDataControl.Dock = DockStyle.Fill;
        }
        else
        {
            lstTables.Visible = true;
            grdSampleDataControl.Dock = DockStyle.None;
        }
        bSelectionChanged = true;
        dTable.Dispose();
        mCon.Close();
        openFileDialog1.Dispose();

It is getting failed here wiht exception\

The Microsoft Office Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data.

But file is not opened anywhere ??

Edit

On debug, When it throws exception at mCon.Open(), If i press F10 compiler moves to next statement and run the programm succesfully. why it is so ??

Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83

1 Answers1

0

This is happening because your Acccess Database file is open.

You can not keep file open, and do changes on that file dynamically.

While the program using that file is running, it tries to open that file.

But, if file is already open exclusively by user, then it fails.

Close the file, and then try to open the connection.

For other details on it, follow this DISCUSSION.

Freelancer
  • 9,008
  • 7
  • 42
  • 81