Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am getting this error
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

on this line of codes :-
DialogResult dr = openFileDialog1.ShowDialog();//error is here
          if (dr == DialogResult.OK)
          {
              label6.Text = openFileDialog1.SafeFileName;
              pictureBox1.BackgroundImage = Image.FromFile(openFileDialog1.FileName);
          }

I searched on google and find 100+ searched result and I read most of them, but I couldn't find proper solution on this.
I also change setting in VS2010 which is Option->Debugging -> unchecked "supress JIT optimization on module load(Managed Only)".
Still my error couldn't resolved ..

Please Help me to out..

Regards
Jayanta
Posted
Comments
emcp 10-May-14 9:39am    
Does OpenFileDialog class work in an empty new project?
JayantaChatterjee 10-May-14 9:43am    
Sorry I don't understand...
emcp 10-May-14 10:06am    
OpenFileDialog is a built in class, and calling ShowDialog is throwing a memory exception. So making a new project and adding an OpenFileDialog, and calling ShowDialog, will rule out that anything is corrupted.

Otherwise, ensure you are not using a custom OpenFileDialog class.

Then check that the default folder, properties etc.. are not assigned to something that could throw an exception. However that would seem unlikely.
JayantaChatterjee 10-May-14 10:14am    
OpenFileDialog is Build in Class(which .NET provide).
I checked OpenFileDialog in different project it worked fine.
The InitialDirectory property of OpenFileDialog is empty...
emcp 10-May-14 10:52am    
try reinstanciate your dialog:
OpenFileDialog dlgOpen = new OpenFileDialog();
DialogResult dr = dlgOpen.ShowDialog();

1 solution

Start with the debugger: put a breakpoint on the ShowDialog line and run you app. when it reaches the line, the debugger will stop and wait for you. Look closely at the OpenFileDialog and make sure everything looks reasonable. Then look at the preceding lines of code and see if there is anything odd (or outside .NET) in that. If everything looks ok, single step execute the line and see what happens.

There is no simple "do this" fix for problems like this: as the error says, "This is often an indication that other memory is corrupt" - and that means trying to find out what, and where, and how before you can move on. So start by getting information, because you are going to need it!
 
Share this answer
 
Comments
JayantaChatterjee 10-May-14 10:04am    
this is the first line of button_click event in my project.
I also add break point to that line and debugging my project I found nothing suspicious , but same error occurred. :-(
OriginalGriff 10-May-14 10:11am    
Then you need to look at what else you application is doing - because .NET doesn't normally throw exceptions on OpenFileDialogs (I use them all the time)

So look at what else your app is doing before you press the button - and if you can't spot anything start commenting out the form Load event, anything clever in the constructor, any timers, etc. (You can highlight it and use CTRL+K,C to comment a block of code, and CTRL+K,U to uncomment it).

When you have the error gone, start looking at teh code you removed, and put it back gradually.
JayantaChatterjee 10-May-14 10:21am    
Thank You Sir..
I got the error generator code in my application, which is :
Data_Con Modi = new Data_Con();
if (paraVal != null)
{
Modi.SelectSta("select * from custDetails where custName='" + textBox1.Text + "'", "custDetails");
if (Modi.dataSet.Tables["custDetails"].Rows.Count > 0)
{
textBox2.Text = Modi.dataSet.Tables["custDetails"].Rows[0][2].ToString();
textBox3.Text = Modi.dataSet.Tables["custDetails"].Rows[0][3].ToString();
textBox4.Text = Modi.dataSet.Tables["custDetails"].Rows[0][4].ToString();
textBox5.Text = Modi.dataSet.Tables["custDetails"].Rows[0][0].ToString();
}
}
Modi.Dispose();
This code is in TextBox_leave event..
I create Dispose method in that class(Data_Con). close and dispose all the object in that class..
so now what i need to do??
OriginalGriff 10-May-14 10:27am    
For starters, don't do that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

I'm guessing it's the Dispose - so start by removing just that line and see if the problem goes away. If it does, then you need to look at what you are doing in the Dispose method.
JayantaChatterjee 10-May-14 10:39am    
Thanks for Remind me about SQL Injection, I will Use Parametrized Query..

After making comments on those lines its work properly, that's why I posted that lines of codes.
In Data_Con class dispose method code is :
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
try
{
if (disposing)
{
conn.Close();
conn.Dispose();
cmd.Dispose();
adpt.Dispose();
dataSet.Dispose();
}
}
catch { }
}
Is this wrong ?

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