Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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

C#
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
Updated 10-Oct-12 19:14pm
v3

Add the STAThreadAttribute attribute on the Main method.

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

For example,

C#
[STAThread]
static void Main(string[] args)
{
    // rest of the code goes here
}
 
Share this answer
 
Comments
[no name] 11-Oct-12 1:29am    
I can't able to get you.could you elaborate the answer...I'm working on C# i.e windows form application.
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?
[no name] 11-Oct-12 4:41am    
Ya i have seen d link and getting confused of it...Could you send me the exact coding for uploading file in C#.
CodingLover 11-Oct-12 5:20am    
You mean upload file or browsing a file for selection?
I solved myself by using the below code:
private void btnBrowseFile_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900