0

I'm working on a windows form project and it creates a PDF file. I want to save file where user want it to. I use a SaveFileDialog for this. When I click "Save as a PDF" button on my form I get this error code.

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

If I don't use the SaveFileDialog (If I give a static name for the file), don't get the error.

Here is button click code:

    private void button2_Click(object sender, EventArgs e)
    {
        SaveFileDialog saveFileDialog1 = new SaveFileDialog();
        saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
        saveFileDialog1.Filter = "(*.pdf)|*.pdf|All Files (*.*)|*.*";
        saveFileDialog1.FilterIndex = 1;
        saveFileDialog1.ShowDialog();


        if (saveFileDialog1.FileName != "")
        {
            iTextSharp.text.Document pdfDosya = new iTextSharp.text.Document(PageSize.A4, 20, 20, 10, 10);
            PdfWriter.GetInstance(pdfDosya, new FileStream(saveFileDialog1.FileName, FileMode.Create));//TODO dosya ismi
            pdfDosya.Open();
        }
     }

How can I solve the problem.

Erdinç Özdemir
  • 1,363
  • 4
  • 24
  • 52
  • 3
    Just a side note (not the answer to your question). Use `if(saveFileDialog1.ShowDialog() == DialogResult.OK)` instead of `if (saveFileDialog1.FileName != "")`. – Sina Iravanian Apr 18 '13 at 11:15
  • Also, `Convert.ToString(Environment.SpecialFolder.MyDocuments) == "MyDocuments"`, doesn't yield a path. – H H Apr 18 '13 at 11:19
  • 1
    Can you post the stack trace from the exception? – H H Apr 18 '13 at 11:24
  • In which line you getting the error? – Praveen VR Apr 18 '13 at 11:24
  • @HenkHolterman I added try catch for saveFileDialog1.ShowDialog() line. I get this error now. "An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll" – Erdinç Özdemir Apr 18 '13 at 11:29
  • @PraveenVR "saveFileDialog1.ShowDialog();" this line. – Erdinç Özdemir Apr 18 '13 at 11:29
  • Have you tried commenting out the properties of the save file dialog object to see if any of them are causing problems? Start with commenting away all of them. Does that work? Then reintroduce one by one. – krembanan Apr 18 '13 at 11:52
  • tried all the combinations but still having the same problem. – Erdinç Özdemir Apr 18 '13 at 11:59
  • You need to isolate this - make the smallest example possible - which does nothing else (prior to calling the Save dialog) but this. See if it happens still. If it doesn't it means something's done before that's the issue. If it repeats then you have a clear small example you can post or test further – NSGaga-mostly-inactive Apr 18 '13 at 12:15
  • What is `PdfWriter`? Is it 3rd party component? Is it .NET library? – Maciej Apr 18 '13 at 12:23
  • @NSGaga should I include the ITextSharp in this project or not? – Erdinç Özdemir Apr 18 '13 at 13:20
  • @Maciej it's an ITextSharp library type. – Erdinç Özdemir Apr 18 '13 at 13:21
  • try both ways - but unless you have something else dealing with unmanaged code then you'll probably need it. Try it and see - cut just to that section that loads and saves the file. If still problems, then try w/o ITextSharp. That's the best (not gonna say the 'only') way to deal w/ these problems. – NSGaga-mostly-inactive Apr 18 '13 at 13:23
  • @NSGaga I tried the both ways. I created a new project. Added only a button into it. I added the codes I writed below. And it works fine. The problem is in my real project. But I don't what I must do to solve it? It gives me a headache! – Erdinç Özdemir Apr 18 '13 at 13:40
  • 2
    :) welcome to coding - you keep 'adding' until you see what's the 'point of bother' - start from that 'working minimal' and add things that your 'real project' has - but one by one - until it doesn't work again. There're tools to help but I don't know from memory, it was a long time. – NSGaga-mostly-inactive Apr 18 '13 at 13:44
  • @NSGaga thanks for your help. I'll try them to find solution. – Erdinç Özdemir Apr 18 '13 at 13:55

2 Answers2

0

This could be the problem:

You can't use Convert.ToString(Environment.SpecialFolder.MyDocuments) to get the folder path as a string, it's only a enum. You need to use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) to get the path as a string for the enum value.

Fox32
  • 13,126
  • 9
  • 50
  • 71
  • 2
    This can get you the wrong folder but not "Attempted to read or write protected memory" – H H Apr 18 '13 at 11:23
0

Try this code

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.InitialDirectory =   Convert.ToString(Environment.SpecialFolder.MyDocuments);
    saveFileDialog1.Filter = "(*.pdf)|*.pdf|All Files (*.*)|*.*";
    saveFileDialog1.FilterIndex = 1;

   if(saveFileDialog1.ShowDialog() == DialogResult.OK) 
   {
        MemoryStream ms = new MemoryStream();
        iTextSharp.text.Document document = new Document(PageSize.A4, 10.0F, 10.0F,100.0F,0.0F);
        document.AddTitle("FileName.pdf");
        PdfWriter writer = PdfWriter.GetInstance(document, ms);
        document.Open();
    }
Praveen VR
  • 1,554
  • 2
  • 16
  • 34