Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello members,
i m getting an exception while working with OpenFileDialog,following is the code

C#
private void btnBrowse_Click(object sender, EventArgs e)
       {
           OpenFileDialog f = new OpenFileDialog();
           if(f.ShowDialog()==DialogResult.OK)
           {
               txtDoc.Text = f.FileName;
           }
       }



and the exception message is:

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.


plz help me...why m i getting this msg...i have included all the necessary namespaces...
Posted

1 solution

Hi,

it's needed to run OLE calls (like the OpenFileDialog) in a Single Thread (STA), your thread is running in Multithread (MTA).

Just add [STAThread] to your main() method.

Regards
 
Share this answer
 
Comments
[no name] 5-Mar-12 7:51am    
how..???
[no name] 5-Mar-12 7:54am    
this is my main program:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace College_managemet_System
{
static class Program
{
///
/// The main entry point for the application.
///

//[STAThread]
public static SqlConnection con;
public static void Connection2Server()
{
con = new SqlConnection();
con.ConnectionString = "Data Source=DINESH; Initial Catalog=College_Management_System; User ID=sa; Password=niitvasai";
con.Open();
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


tell me where the problem is....
El_Codero 5-Mar-12 8:01am    
The STAThrea Attribute hast to be set explicit to the entry point or method.

static class Program
{
///
/// The main entry point for the application.
///

public static SqlConnection con;
public static void Connection2Server()
{
con = new SqlConnection();
con.ConnectionString = "Data Source=DINESH; Initial Catalog=College_Management_System; User ID=sa; Password=niitvasai";
con.Open();
}

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
El_Codero 5-Mar-12 7:57am    
in your program.cs:

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
[no name] 5-Mar-12 8:13am    
you mean to say,just above the main method starts...???

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