Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open Save File Dialog Error - ThreadStateException

Status
Not open for further replies.

dpimental

Programmer
Jul 23, 2002
535
US
When I try to run an OpenFileDialog or a SaveFileDialog from my asp.net web page I get an exception that says ...

Code:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. 
Ensure that your Main function has STAThreadAttribute marked on it. 
This exception is only raised if a debugger is attached to the process.

Is there another way of getting past this exception, other then manually setting a single thread and calling my method from there (see code below).

Code:
Thread newThread = new Thread(new ThreadStart(ThreadMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();

static void ThreadMethod()
    {
        System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
        sfd.Filter = "XML | *.xml";
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string name = sfd.FileName;
            string cnt = File.ReadAllText("C:\\xml_files\\data.xml");
            File.WriteAllText(name, cnt);
        }
    }


David Pimental
(US, Oh)
 
I am not sure what your question is
However, you say this is an ASP.NET application? If so, this code will not work. Well, it may, just not as you expect. If a dialog box does show, it will show on the server NOT the client machine. It may work when you test locally, if our machine is both server and client, however, it will never work as expected in a production environment.

If you want to display a message box to the user, you will need to use a javascript function called alert().
 
Here is what I am trying to do.

I have 2 reasons for the file dialog.
1. To save an xml file (the user will decide where).
2. To browse to select a pdf file to validate it's pagination.

My application is a web application by I reference the windows dialogs to accomplish the tasks above.
The problem is, according to the documentation, it requires a single thread apartment.
I have a work around for that (see code example above); but I need to be able to update the Web Interface from a separate thread which is a different problem.

Any suggestions?

David Pimental
(US, Oh)
 
Like I said. the dialog will NOT show on the client, it will show on the server.. You will need to use a file upload control to upload a file to the server. To show a warning message or something like that, you need to use javascript and use an alert box.
 
to open save dialog from web page you need

Code:
Response.Clear
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
response.write(filecontent);

just set filename value and filecontent before and you are all set
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top