简体   繁体   中英

How to print multiple instance of crystal report using single Crystal Report Viewer in c#


I am trying to print multiple crystal report using single Crystal Report Viewer. I have 'n' number of items in a database and I need to print 'n' crystal reports. As it is dynamic in nature so I cannot fix number of Report Viewer, so I thought to use single Report Viewer and load crystal report using 'For loop'.
I created a new datatable in dataset and I was trying to put value from another datatable it didnt worked out so created parameter fields and is putting values in through

For Loop

Here my code(I have 3 parameter fields for ease of viewing showing only one):

private void CRVtakeout_Load(object sender, EventArgs e)
    {
      try
           {
            string sqlqry = "Select KOTNo,Time,TableNo,WaiterName,ItemCode,ItemName,Quantity,Amount,Foodtype From tblOrder Where KOTNo=@kotno";
            SqlCommand cmd = new SqlCommand(sqlqry, connectionclass.con);
            cmd.Parameters.AddWithValue("@kotno", NewOrderBL.KOTNo);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataSet1 ds = new DataSet1();


            adapter.Fill(ds, "Takeout");
            //adapter.Fill(ds, "Takeout");
            string tableno = ds.Tables["Takeout"].Rows[i][2].ToString();
            tableno = tableno.Substring(6, 1);
            if (ds.Tables["Takeout"].Rows.Count == 0)

            {
                MessageBox.Show("No Data Found", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (tableno == "T")
            {
                for (int i = 0; i < ds.Tables["Takeout"].Rows.Count; i++)
                {
                    crystalReportViewer1.RefreshReport();  
                ParameterFields paramFields = new ParameterFields();
                ParameterField paramField = new ParameterField();
                ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
                paramField.Name = "Billno";
                paramDiscreteValue.Value = ds.Tables["Takeout"].Rows[0][0].ToString();
                paramField.CurrentValues.Add(paramDiscreteValue);
                paramFields.Add(paramField);
printtakeout printtakeout = new printtakeout();
                printtakeout.SetDataSource(ds);
                crystalReportViewer1.ReportSource = printtakeout;
                System.Drawing.Printing.PrintDocument printdocument = new System.Drawing.Printing.PrintDocument();
                printtakeout.PrintOptions.PrinterName = printdocument.PrinterSettings.PrinterName;
                printtakeout.PrintOptions.PrinterName = "EPSON TM-U220 Receipt";
                printtakeout.PrintToPrinter(1, false, 0, 0);

                   }
  }
 }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        finally { connectionclass.disconnect(); }**strong text**
}

Each time I do it gives error :

activex control 8856f961-340a-11d0-a96b-00c04fd705a2 cannot be instantiated because the current thread is not a single-thread apartment

If I dont use parameter field theirs no error. If any clarification needed please do tell. Thanks .

There is an example to do queries

using (SqlConnection connection = new SqlConnection("connectionString"))
            {
                connection.Open();
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT * FROM Customers";
                command.CommandType = CommandType.Text;

                using (SqlDataReader objDataReader = command.ExecuteReader())
                {
                    if (objDataReader != null)
                    {
                        while (objDataReader.Read())
                        {
                          // To get results...
                          // objDataReader["NameResultParam"];
                        }
                    }
                }
          }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM