Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm working on a windows WPF application.

The thing is I have an AccessViolationException when working with both OpenFileDialog and OleDbConnection objects.

Scenario: After running the application, I click on a button that display the OpenFileDialog form in order to browse for a file. After that I'm using an OleDbConnection in order to reference an MS Access .accdb file.

Problem: Running this scenario for the first time, everything works well, but trying to browse the file or any other file for the second time the exception occurs; it occurs after the form is opened and the file is selected.

After debugging the code, I have found that when trying to open the connection of the OleDbConnection object [ex: dbConnecntion.Open()] the exception occurs but if omitted it disappears, even if I'm closing the connection and disposing the resources.

Exception: AccessViolationException >> Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Posted
Updated 8-Aug-18 3:48am
v2

I just spent the past week trying to get rid of this same issue. Finally found this post which indicates that it's a bug in Microsoft Access Database Engine 2010 drivers. If you use the 2007 drivers instead, the problem goes away.
 
Share this answer
 
Comments
Nish Nishant 17-Jan-11 8:53am    
Proposed as answer.
aydinsahin 18-Apr-11 9:07am    
thanks,
i tried solution and installed AccessDatabaseEngine.exe for 2007. problem removed
Take a look at:

https://connect.microsoft.com/VisualStudio/feedback/details/624503/oledb-operations-cause-accessviolationexception-during-savefiledialog?wa=wsignin1.0

And please vote for a fix - using the I can reproduce this bug and the green tick button
 
Share this answer
 
Comments
Nish Nishant 17-Jan-11 8:53am    
Proposed as alternate answer.
the key of this questiuon is "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DbTest\Test.accdb; OLE DB Services=-1" check this out if it was true send me mail naavid2000@yahoo.com

(OLE DB Services=-1)
 
Share this answer
 
Comments
ronit12123 9-May-13 6:33am    
It works! Thanks a ton!!!
JayantaChatterjee 3-Feb-15 9:52am    
It's works for me....
Finally I get rid of this error..
Thanks a Lotttttttttttttttt....
tony4250 19-Mar-15 16:58pm    
Works me to,
Thanks!!!
Member 10765423 18-Apr-15 3:40am    
Work
Cresmx 29-Jul-15 18:30pm    
It also worked for me. Thank you.
After a read or write operation, you have to always close the file.
 
Share this answer
 
v2
Comments
ziadb86 2-Sep-10 16:30pm    
I'm closing the connection and disposing its resources. The exception is produced only when I open the connection. If I omit oledbConnection.Open() the exception wont be raised.
Im having a similar problem. I was able to reproduce it. I create new windows form, add a new data source (an access data base file, with only one table). Then drag the table to the form. Then add a new button and the following code.

private void Test()
{
	this.table1TableAdapter.GetData();
}

private void SaveDialogTest()
{

	SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "Text File|*.txt";
        saveFileDialog1.InitialDirectory = Application.StartupPath;
        saveFileDialog1.Title = "test";
        saveFileDialog1.ShowDialog();:mad:

        if (saveFileDialog1.FileName != "")
                 { }
}

private void button1_Click(object sender, EventArgs e)
{
        Test();
	SaveDialogTest();           
}


The table adapter is created by the designer:

private void Form1_Load(object sender, EventArgs e)
{
            // TODO: This line of code loads data into the 'testDataSet.Table1' table. You can move, or remove it, as needed.
            this.table1TableAdapter.Fill(this.testDataSet.Table1);

}


When I run the form and click on the button I get an AccessViolationException, when calling saveFileDialog1.ShowDialog().
If i move the "Test()" method inside the "Form1_Load" method everything works fine.

Also if I run the dialog first, before calling GetData() everything works fine afterwards. Like this.

SaveDialogTest();
Test();
SaveDialogTest();

This will call the second SaveDialogTest() without exception.
Also if I compile it and run it in and XP pc everything works fine.

Data Source: Microsoft Access Database File (OLE DB)
Im using Visual Studio 2008 SP1. Windows 7
 
Share this answer
 
v3
I had the same problem; re-installation of Access DB Engine 2010 solved the problem.

Funnily, I could not even catch the exception, the window simply went blank.
 
Share this answer
 
v2
XML
On windows 7 (32 bit) had same problem when showing the openfiledialog from a modal form loaded for the second time with data from db in it. Solution appeared to be: do not allow autoupgrade of dialog.
Dim sfv As New System.Windows.Forms.SaveFileDialog
   With sfv
     .AutoUpgradeEnabled = False
     [...]


But error came up again. Then I noticed it was apparently randomic till I realized it did not come out ifd I was able to show a saveFileDialog or an OpenfileDialog before loading any data from db.

Thus true workaround is: before load anything on the form you're going to show, display a dialog asking user to choose a path and file you *might* need after (arrrg!). After that, load data. Now your can let users, if needed, to choose path and file with dialog again...

ie:
  Private Sub frmReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     txtFilePathName.Text = "Export_" & Now.ToString("yyyy_MM_dd_HH_mm_ss", CultureInfo.GetCultureInfo("it-It")) & ".csv"
     txtFilePathName.Text = GetSaveFileName(txtFilePathName.Text, ".csv", "Choose a csv File to save exported data", "csv |*.csv|All |*.*")
     'now load data in forms, where you can also have a button to call again the GetSaveFileName
[...]

Private Function GetSaveFileName(ByVal fileName As String,
                                    ByVal defaultExtension As String,
                                   ByVal title As String,
                                   ByVal filter As String) As String
        Dim sfv As New System.Windows.Forms.SaveFileDialog
        With sfv
            .RestoreDirectory = True
            .AddExtension = True
            .DefaultExt = defaultExtension
            .FileName = fileName
            .Title = title
            .Filter = filter
            .CheckPathExists = True
            .OverwritePrompt = True
            .ShowHelp = False

            If (.ShowDialog = DialogResult.OK) Then
                fileName = .FileName
            End If
        End With
        Return fileName
    End Function
Cimperiali
 
Share this answer
 
v2
Comments
vijayr 17-May-11 9:08am    
@Cimperiali: i am also facing the same issue with AccessViolationException and your suggestion is fine and it's working good.
Here My Question, Why it's throwing the error in OpenFileDialog ? because there is no connectivity between database and OpenFileDialog.
And also i found one more solution(but it's not good)
Here the code is

public class OpenDialogBox
{
public string _filter { get; set; }
public string _selectedFile { get; set; }
public System.Windows.Forms.DialogResult _dialog { get; set; }

public void OpenDig()
{
try
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(Folder));
th.IsBackground = true;
th.Start();
System.Threading.Thread.Sleep(1000);
if (th.IsAlive)
{
th.Abort();
}
}
catch (System.Threading.ThreadAbortException ex)
{

}
//Folder();
System.Windows.Forms.OpenFileDialog OF = new System.Windows.Forms.OpenFileDialog();
OF.Filter = _filter;
OF.AutoUpgradeEnabled = false;
OF.InitialDirectory = "C:\\";
_dialog = OF.ShowDialog();
_selectedFile = OF.FileName;
//System.Windows.Forms.MessageBox.Show(_selectedFile);
//System.Windows.Forms.MessageBox.Show(OF.FileName.ToString());
}

void Folder()
{
try
{
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowDialog();
}
catch (Exception ex)
{

}

}
}
Hi,

Create a dll for the below code and call it while you clicking the browse button


public class OpenDialogBox { public string _filter { get; set; } public string _selectedFile { get; set; } public System.Windows.Forms.DialogResult _dialog { get; set; } public void OpenDig() { try { System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(Folder)); th.IsBackground = true; th.Start(); System.Threading.Thread.Sleep(1000); if (th.IsAlive) { th.Abort(); } } catch (System.Threading.ThreadAbortException ex) { } //Folder(); System.Windows.Forms.OpenFileDialog OF = new System.Windows.Forms.OpenFileDialog(); OF.Filter = _filter; OF.AutoUpgradeEnabled = false; OF.InitialDirectory = "C:\\"; _dialog = OF.ShowDialog(); _selectedFile = OF.FileName; //System.Windows.Forms.MessageBox.Show(_selectedFile); //System.Windows.Forms.MessageBox.Show(OF.FileName.ToString()); } void Folder() { try { System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog(); fd.ShowDialog(); } catch (Exception ex) { } } }


Thanks
Vijay r
 
Share this answer
 
Hi.
I also had this problem.Remove previous Microsoft Access Database Engine 2010 and download new update from this link http://www.microsoft.com/en-us/download/details.aspx?id=13255[^]

It worked good for me.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900