1

I am using windows application here is my piece of code where i Get AccessviolationException exception and error : attempted to read or write protected memory which is most common error. Main problem is i get this error after execution of thhis code 2-3 times..

 private void btnBrowse_Click(object sender, EventArgs e)
        {           

                openFileDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv;*.txt";

                openFileDialog.ShowDialog();   // here i get  above eoor after this method execute 2-3 time not before that (on development machine)             
        }

When i tranfer EXE file to Client's machine, on btnBrowse_Click suddenly Application crashes and stopped working.. not even work one time.

What i have tried so far??

1) checked check box "Enable unmanaged code debugging" in project properties
2) tried to handle AccessViolationexception error using [HandleProcessCorruptedStateExceptions] and try catch ..

How to fix this?? enter image description here

Dharti Sojitra
  • 207
  • 1
  • 10
  • openFileDialog is created in code or using the Designer ? Is there any other code that references openFileDialog ? – GuidoG Aug 31 '16 at 12:43
  • i used openDialog using designer.. no there is no other code except its `openDialog_FileOk` event – Dharti Sojitra Aug 31 '16 at 12:45
  • when the error appears on your development machine, does it occurs on the code where you set the filter or on the showdialog ? – GuidoG Aug 31 '16 at 12:46
  • already described in questions at this line `openFileDialog.ShowDialog();` – Dharti Sojitra Aug 31 '16 at 12:47
  • is this the entire code or did you leave parts out to simplify the question ? If so than post the entire code in btnBrowse_Click – GuidoG Aug 31 '16 at 12:50
  • Are you by any chance using some kind of OleDB Database connection (for example like an Microsoft Access Database)? In that case [this link might help fixing this problem](https://social.msdn.microsoft.com/Forums/vstudio/en-US/ac25ad67-6cce-44de-b105-ee8f6a190a88/openfiledialog-unhandled-systemaccessviolationexception-only-over-windows-7-32bits?forum=csharpgeneral) – bassfader Aug 31 '16 at 13:01
  • Yes I am using access database and i didnt find any solution there – Dharti Sojitra Aug 31 '16 at 13:03
  • What exactly did you try? Have you tested if it is related (i.e. temporarily remove the OleDB connection to see if thats the cause)? Have you installed the hotfix (KB2516475) mentioned in the first link of the accepted answer? Have you tried changing the connection string (i.e. switching from OLEDB 12 to OLEDB 4, or adding the `OLE DB services=-1` statement there)? – bassfader Aug 31 '16 at 13:11
  • No i didnt tried before tried now 1) switching from OLEDB 12 to OLEDB 4, or adding the OLE DB services=-1 tried didnt work,, After removing oledb still get same error, about hotfix i dont know what exactly it is.. – Dharti Sojitra Aug 31 '16 at 13:23
  • Can you try creating a new OpenFileDialog in code inside btnBrowse_Click and not use the one created with the designer. – GuidoG Aug 31 '16 at 13:34
  • Two suggestions: 1) remove the semicolon after the the `*.txt`. 2) Make sure that `openDialog.FilterIndex` is set to 1. – Jim Mischel Aug 31 '16 at 13:58
  • This guy had the same issue: https://social.msdn.microsoft.com/Forums/vstudio/en-US/ac25ad67-6cce-44de-b105-ee8f6a190a88/openfiledialog-unhandled-systemaccessviolationexception-only-over-windows-7-32bits?forum=csharpgeneral – Cadburry Aug 31 '16 at 15:20
  • @GuidoG ,@Jim Mischel Sorry to say but this solutions are not working.. i can not remove semicolon because in c# it is compulsory at the end of line Ow i will get error and `openDialog.FilterIndex` is set to 1. this didnt work , creating OpenFileDialog in code inside btnBrowse_Click is not working i get same error.. – Dharti Sojitra Sep 01 '16 at 05:39
  • Are you doing anything else with that `OpenFileDialog`? It sounds like something in your code somewhere is setting a value that affects later operation. You probably should go through your code and closely examine every line that references `openDialog`. – Jim Mischel Sep 01 '16 at 14:31
  • @Jim Mischel `openDialog.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm;*.csv;*.txt"; openDialog.ShowDialog(); filePath = openDialog.FileName;` at the end `openDialog.Dispose();` i have used openDialog only in these lines . nowhere else – Dharti Sojitra Sep 02 '16 at 05:28

0 Answers0