Cross-thread operation not valid: Control 'errorBox' accessed from a thread other than the thread it was created on.
-
Hi I am trying to create subreport using VS-2012 RDLC. Does anybody have faced the same above problem while calling subreport in RDLC report. What I am trying to acheive is as stated below I have 2 tables 1) BankAccount & 2)BankAccountDetail. The relation between the two is by using bankCode field. Now I want to display report which show records from BankAccount table (Header) followed by the detail records from BankAccountDetail table (Detail) in subreport. Below is what I have coded into winforms, but it raises an error stating cross-thread exception. private void button1_Click(object sender, EventArgs e) { sqlAdapt.Fill(ds, "BankAccount"); this.reportViewer1.Reset(); this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\repBank.rdlc"; this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DSBankAccount",ds.Tables["BankAccount"])); this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); reportViewer1.DocumentMapCollapsed = true; reportViewer1.RefreshReport(); } void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) { SqlConnection sqlConn = new SqlConnection(str); string query = "select * From BankAccountDetails"; sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand(query, sqlConn); sqlCmd.Parameters.Add(new SqlParameter("@BankCode", DbType.Int32)).Value = "1"; sqlCmd.CommandText = query + " where BankCode = " + new SqlParameter("@BankCode", DbType.Int32); SqlDataAdapter sqlAdapt = new SqlDataAdapter(sqlCmd); DataSet ds = new DataSet(); sqlAdapt.Fill(ds, "BankAccountDetails"); this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\subRptBankDetail.rdlc"; ReportParameter[] param1 = new ReportParameter[1]; param1[0] = new ReportParameter("BankCode", "1"); this.reportViewer1.LocalReport.SetParameters(param1); reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables["BankAccountDetails"])); this.reportViewer1.DocumentMapCollapsed = true; //BELOW LINE RAISES AN ERROR CROSS THREAD OPERATION
-
Hi I am trying to create subreport using VS-2012 RDLC. Does anybody have faced the same above problem while calling subreport in RDLC report. What I am trying to acheive is as stated below I have 2 tables 1) BankAccount & 2)BankAccountDetail. The relation between the two is by using bankCode field. Now I want to display report which show records from BankAccount table (Header) followed by the detail records from BankAccountDetail table (Detail) in subreport. Below is what I have coded into winforms, but it raises an error stating cross-thread exception. private void button1_Click(object sender, EventArgs e) { sqlAdapt.Fill(ds, "BankAccount"); this.reportViewer1.Reset(); this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\repBank.rdlc"; this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DSBankAccount",ds.Tables["BankAccount"])); this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); reportViewer1.DocumentMapCollapsed = true; reportViewer1.RefreshReport(); } void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) { SqlConnection sqlConn = new SqlConnection(str); string query = "select * From BankAccountDetails"; sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand(query, sqlConn); sqlCmd.Parameters.Add(new SqlParameter("@BankCode", DbType.Int32)).Value = "1"; sqlCmd.CommandText = query + " where BankCode = " + new SqlParameter("@BankCode", DbType.Int32); SqlDataAdapter sqlAdapt = new SqlDataAdapter(sqlCmd); DataSet ds = new DataSet(); sqlAdapt.Fill(ds, "BankAccountDetails"); this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\subRptBankDetail.rdlc"; ReportParameter[] param1 = new ReportParameter[1]; param1[0] = new ReportParameter("BankCode", "1"); this.reportViewer1.LocalReport.SetParameters(param1); reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables["BankAccountDetails"])); this.reportViewer1.DocumentMapCollapsed = true; //BELOW LINE RAISES AN ERROR CROSS THREAD OPERATION
You haven't posted the relevant code. The Exception occurs when you try to access a Control from a Thread that is different from the Thread that created it. The code you posted does not access the 'erroBox' control, so you've missed the relevant code. You'll need to use code similar to this:
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
} -
Hi I am trying to create subreport using VS-2012 RDLC. Does anybody have faced the same above problem while calling subreport in RDLC report. What I am trying to acheive is as stated below I have 2 tables 1) BankAccount & 2)BankAccountDetail. The relation between the two is by using bankCode field. Now I want to display report which show records from BankAccount table (Header) followed by the detail records from BankAccountDetail table (Detail) in subreport. Below is what I have coded into winforms, but it raises an error stating cross-thread exception. private void button1_Click(object sender, EventArgs e) { sqlAdapt.Fill(ds, "BankAccount"); this.reportViewer1.Reset(); this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local; this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\repBank.rdlc"; this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DSBankAccount",ds.Tables["BankAccount"])); this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); reportViewer1.DocumentMapCollapsed = true; reportViewer1.RefreshReport(); } void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e) { SqlConnection sqlConn = new SqlConnection(str); string query = "select * From BankAccountDetails"; sqlConn.Open(); SqlCommand sqlCmd = new SqlCommand(query, sqlConn); sqlCmd.Parameters.Add(new SqlParameter("@BankCode", DbType.Int32)).Value = "1"; sqlCmd.CommandText = query + " where BankCode = " + new SqlParameter("@BankCode", DbType.Int32); SqlDataAdapter sqlAdapt = new SqlDataAdapter(sqlCmd); DataSet ds = new DataSet(); sqlAdapt.Fill(ds, "BankAccountDetails"); this.reportViewer1.LocalReport.ReportPath = @"D:\vs 2012\RDLC\IBF\IBF\subRptBankDetail.rdlc"; ReportParameter[] param1 = new ReportParameter[1]; param1[0] = new ReportParameter("BankCode", "1"); this.reportViewer1.LocalReport.SetParameters(param1); reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1", ds.Tables["BankAccountDetails"])); this.reportViewer1.DocumentMapCollapsed = true; //BELOW LINE RAISES AN ERROR CROSS THREAD OPERATION
-
WRONG! Everything will NOT run perfectly. Setting that option to false opens you up to all kinds of little bugs that are intermittent and just about impossible to reproduce. NEVER NEVER NEVER set that to false. Do control manipulation from non-UI threads correctly in your code and you never have to worry about those problems.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak