Click here to Skip to main content
12,300,245 members (45,170 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C#
Hi,

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 18:47pm
Edited 10-Oct-12 19:14pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Add the STAThreadAttribute attribute on the Main method.

http://msdn2.microsoft.com/en-us/library/system.stathreadattribute(vs.71).aspx[^]

For example,

[STAThread]
static void Main(string[] args)
{
    // rest of the code goes here
}
  Permalink  
Comments
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
 
good
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)
{
openFileDialog1.ShowDialog();
}
  Permalink  

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
Web01 | 2.8.160525.2 | Last Updated 11 Oct 2012
Copyright © CodeProject, 1999-2016
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