×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Open Save File Dialog Error - ThreadStateException

Open Save File Dialog Error - ThreadStateException

Open Save File Dialog Error - ThreadStateException

(OP)
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)

RE: Open Save File Dialog Error - ThreadStateException

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().

RE: Open Save File Dialog Error - ThreadStateException

(OP)
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)

RE: Open Save File Dialog Error - ThreadStateException

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.

RE: Open Save File Dialog Error - ThreadStateException

to open save dialog from web page you need

CODE --> C#

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close