Click here to Skip to main content
12,823,984 members (43,663 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#3.0 C#

When i'm using the below code for Browse Button.

if (openFileDialog1.ShowDialog() == DialogResult.OK)
    textBox1.Text = openFileDialog1.FileName;

I'm getting the below error:
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
Posted 10-Oct-12 19:47pm
Updated 10-Oct-12 20:14pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Add the STAThreadAttribute attribute on the Main method.[^]

For example,

static void Main(string[] args)
    // rest of the code goes here
CodingLover 11-Oct-12 3:49am
Did you read the link that I sent you?

Even a win form application, in program.cs file you can find the main method. see how it define. Did you do any changes there?
CodingLover 11-Oct-12 5:20am
You mean upload file or browsing a file for selection?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I solved myself by using the below code:
private void btnBrowseFile_Click(object sender, EventArgs e)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100