Good Morning Guys/Gals.

Need some help with this. How long does it take to dispose of da and confdt?
What im trying to say is, that this function works fine if i execute it every 5 seconds or so, giving it enough time to close the controls, but if I execute this in a short amount of time, i am face with this error: "ERROR [HY000] [Microsoft][ODBC Text Driver] The Microsoft Jet database engine cannot open the file '(unknown)'. It is already opened exclusively by another user, or you need permission to view its data."

Is it really a time issue? Can it be that the process is creating another instance of itself, eventually queuing up in task manager? if it is the process, what would its name be so that i can kill it everytime i need to dispose of it? Or do I need to force a Garbage Collect on it?

private void RowCount()
      {
         DataTable ConfDT = new DataTable();
         string tempPath = @"C:\Download Report Sheets\";
         string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";
         OdbcConnection conn = new OdbcConnection(strConn);
         OdbcDataAdapter da = new OdbcDataAdapter("Select * from Releases.csv", conn);//TestFile
         conn.Open();
         da.Fill(ConfDT);
         ConfigGrid.DataSource = ConfDT;
         ConfigGrid.Columns[0].DefaultCellStyle.Format = "G";
         conn.Close();
      }

Many Thanks

As a matter of good practice I would manually dispose of the elements after each use, not relying on a garbage collection then and should hopefully resolve the error.

but I do close all of my elements with conn.Close(); conn.Dispose(); da.Dispose();. I test this with time periods and I see that it works after I wait a while when executing this, or it bombs out on my 3rd attempt at executing this...

private void RowCount()
      {
         DataTable ConfDT = new DataTable();
         string tempPath = @"C:\Download Report Sheets\";
         string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";
         OdbcConnection conn = new OdbcConnection(strConn);
         OdbcDataAdapter da = new OdbcDataAdapter("Select * from Releases.csv", conn);//TestFile
         conn.Open();
         da.Fill(ConfDT);
         ConfigGrid.DataSource = ConfDT;
         ConfigGrid.Columns[0].DefaultCellStyle.Format = "G";
         conn.Close();
         da.Dispose(); //Try that
         conn.Dispose(); //Try that
      }

You may have already done it but try adding in the two lines at the bottom of the code block.

Technically im not sure why those are crashing as the variables would only live for the lifespan of the method anyway as they are local to it.

The error itself points to the file your trying to open already being open by something else or not closing quick enough within your program. How many times is the program executing this per second?

The time is controlled by me, I click the button when i want, but if i click it too soon then it complains...I wrote something for excel a while ago in which i had to garbage collect an excel interop for it to execute the next time around. This is a csv file that im working on now, and it opens in excel.

I think im going to have to create text file versions of this instead, i don't think notepad will complain about this small issue that im having. Quite strange that i have to wait so long for the elements to close...

It is strange indeed, calling the .Dispose() method is the garbage cleaner for the two variables we call it on so it should work. Stumps me at this point tbh.

Yeah could give it a shot using a .txt though you can also open and edit csv's in notepad :P?

my word...sigh its got nothing to do with it being a txt file or csv...its got to do with the datagrid i think...im still debugging, will update as soon as it is resolved.

Kk lemme know, sorry wasnt of more use

Datagridview's have an event that you can handle that will let you know if the databinding is complete. Something like .DatabindingComplete. If you handle this, have it set a boolean flag to true when you know the control is done with the databinding. This will let you know that the file handle is no longer needed, and things may be disposed of properly. I wouldn't worry too much about manually disposing of these things, the GC knows what it's doing for the most part and unless performance or memory consumption is an issue calling .Dispose() will just clutter your code.

ok its got nothing to do with the datagrid either. If i call the form (known as Configuration) with that control, I click it a couple of times, very fast and nothing happens, so everything is fine with that form, but when i close that form (Configuration) and open it again through the main form (Known as Form1 for the time being), and execute the function again, I get that problem.

Im using base.Dispose(); to close the "Configuration". If i use that command, shouldn't it dispose of everything in "Configuration"? Environment.Dispose(); would close the whole application though, so that's a no go.

I like using the c# keyword using

example

using(DataTable ConfDT = new DataTable())
{
    //some code

} //ConfDT is automatically disposed here

Ok I know what it is, when I step through the program and receive the error, it halts at da.Fill(ConfDT);. I wait a few seconds and step into the next line of code and it works perfectly. The only thing that I can see from this is that the program is trying to access the file but the file is still in the process of being closed, so we have a conflict, up until it closes...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.