Click here to Skip to main content
14,734,309 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I get this error when I try to export or save this report file

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.


What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

namespace EMR.av.FormThamSo
{
    public partial class frm_BAnoikhoa : Form
    {
        public frm_BAnoikhoa()
        {
            InitializeComponent();
        }

        private void btn_inphieu_Click(object sender, EventArgs e)
        {
            string mbn = this.txtMBN.Text;
            
            Report.repBANoiKhoa rep = new Report.repBANoiKhoa(mbn);
            rep.CreateDocument();
            rep.ShowPreviewDialog();
            

        }
    }
}

What am I missing or what was wrong? Will help me! Thank
Posted
Updated 10-Nov-20 23:18pm
v3
Comments
CHill60 11-Nov-20 3:48am
   
Insufficient code supplied to determine what the problem is and why have you tagged your question as LINQ?
It's a good idea to indicate exactly which line throws the exception - I'm guessing in this case that it is the rep.ShowPreviewDialog();?
You need to show how you have set up the threading in your application and also what platform you are using (again, I'm guessing WinForms).
And please do some basic research first - have a look at this thread for some possible solutions : c# - Exception when using FolderBrowserDialog[^]
Huyyhaha 11-Nov-20 4:12am
   
yes it's winform ! I simply want to export the report file to excel or pdf, what do I need to code

1 solution

The error message is pretty clear about the problem and the solution:
Ensure that your Main function has STAThreadAttribute marked on it.
   
Comments
Huyyhaha 11-Nov-20 4:11am
   
I have to do with it
Richard Deeming 11-Nov-20 4:13am
   
Ensure that your Main function has STAThreadAttribute marked on it.
Huyyhaha 11-Nov-20 4:14am
   
I don't understand, can you tell me more clearly
Richard Deeming 11-Nov-20 4:17am
   
Your application has a Main method. You need to ensure it has the [STAThread] attribute on it.
[STAThread]
static void Main(string[] args)
{
    ...
}
Huyyhaha 11-Nov-20 22:14pm
   
I have paragraph code in Program.cs
[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            DevExpress.Skins.SkinManager.EnableFormSkins();
            SkinManager.Default.RegisterAssembly(typeof(DevExpress.UserSkins.BonusSkins).Assembly);
            Application.SetCompatibleTextRenderingDefault(false);
 Application.Run(new ShareProject.frmDangNhap());
            if (ShareProject.Bien.OKLogin)
            {
                ShareProject.Bien.Serial = 0;
                ShareProject.Bien.dondientu_ma_lien_thong_cskcb = "0145679";
                ShareProject.Bien.dondientu_mat_khau_cskcb = "rqULrbcK0N";
                ShareProject.Bien.dondientu_ma_bao_hiem_cskcb = "45679";
                ShareProject.Bien.dondientu_ma_lien_thong_bacsi = "01002245HNO-CCHQ";
                ePubLic.Cap_Do_Khoa_Du_Lieu =4;
                ePubLic.ma_benh_vien_khoa = "88888";
                Application.Run(new Menu());
            }
        }

I'dont know ,Is it correct?
Richard Deeming 12-Nov-20 4:39am
   
I don't know why you've got two Application.Run lines, but otherwise that looks OK.

Are you showing forms from background threads anywhere?
Huyyhaha 12-Nov-20 22:15pm
   
I don't know, this is the first time t encountered this error

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