Hello. I'm trying to print some text using printDocument. I've managed to print out the text I need into a pdf. The text is taken from the database which is initially made when the windows form loads.

The problem is: Once it prints, it shows the pdf, which is fine but when I return back to the program and trigger something that requires the database. It shows an error: "no such table: [tablename]".

I know it's something to do with the current folder location because if I choose the print location to be, for example: Documents and I also place a copy of the database.db3 file in that same location, there are no errors.

I don't think its a good idea to go around copying the db3 file to the location where the print is going to :P

Thanks.

If your program is calling OpenFileDialog() then you need to set the RestoreDirectory property to TRUE so that it does not change the current working directory on you.

I don't think my program is calling OpenFileDialog(), unless printDialog calls it?

This is the code in my button click event:

private: System::Void btnPrint_Click(System::Object^  sender, System::EventArgs^  e) {		 
          if (printDialog1->ShowDialog() == ::DialogResult::OK) {     
               printDocument1->Print();			 
          }		 
}

I don't think my program is calling OpenFileDialog(), unless printDialog calls it?

This is the code in my button click event:

private: System::Void btnPrint_Click(System::Object^  sender, System::EventArgs^  e) {		 
          if (printDialog1->ShowDialog() == ::DialogResult::OK) {     
               printDocument1->Print();			 
          }		 
}

No, my program doesn't call an OpenFileDialog. After the button click event is triggered, the print options window is shown and when I click the pdf printer, it opens up the save file window, which I'm guessing is a "SaveFileDialog". I think that's what is causing the problem, but I don't know how to fix it. I do need that saveFileDialog, however I'm not actually calling it in my code, so I can't set any of its properties.

Does anyone know how to solve this problem?

----------SOLVED---------- Thanks for the help Ancient Dragon

Print Problem: When printing something, the printDialog opens up a saveFileDialog window, which is used to specify the directory in which to save the file to. However, after printing, this directory gets set as the current working directory. Due to the database location not being specified when it is connected to, the program can no longer find the database (.db3) file, so it creates a .db3 file. However, the program cannot perform any of the database related actions due to the database file being empty.

To solve the issue, I created a string to hold the current working directory, before calling the print method and then restoring the directory back to what it was originally.

private: System::Void btnPrint_Click(System::Object^  sender, System::EventArgs^  e) {	
//save the current directory in a string.
String ^path = Environment::CurrentDirectory; 

if (printDialog1->ShowDialog() == ::DialogResult::OK) {  //show printDialog. If OK is clicked do the following        
printDocument1->Print();	//call the print method for the printDocument1, which calls the PrintPage event.

//restore the directory to the original path
Environment::CurrentDirectory = path;
}	
}
commented: Nice work around and thanks for posting the solution. +34
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.