print report without preview ?
  • How can print crystal report without report viewer?
  • 2 Comments sorted by
  • Vote Up0Vote Down studentstudent
    146.00 Karma Accepted Answer
    I am using below code its working.

    private void btnPrint_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();

    ReportDocument crReportDocument;
    crReportDocument = new ReportDocument();
    //Create an instance of a report
    crReportDocument = new KMAAccounts.Reports.GL.rptDebitVoucherS();
    string Query = "exec SP_DebitVoucher @PaymentId,@CompanyCode";
    Query = Query.Replace("@PaymentId", "'" + txtPayment.Text + "'");
    Query = Query.Replace("@CompanyCode", "'" + log.GetCompanyCode() + "'");

    dt = GetDataTable(Query);
    crReportDocument.SetDataSource(dt);
    //Use error handling in case an error occurs
    try
    {
    //Set the printer name to print the report to. By default the sample
    //report does not have a defult printer specified. This will tell the
    //engine to use the specified printer to print the report. Print out
    //a test page (from Printer properties) to get the correct value.

    System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
    crReportDocument.PrintOptions.PrinterName = printDocument.PrinterSettings.PrinterName;


    //Start the printing process. Provide details of the print job
    //using the arguments.
    crReportDocument.PrintToPrinter(1, true, 0, 0);

    //Let the user know that the print job is completed
    MessageBox.Show("Report finished printing!");
    }
    catch (Exception err)
    {
    MessageBox.Show(err.ToString());
    }

    }

    public DataTable GetDataTable(string Query)
    {

    SqlTransaction objTran;//= new SqlTransaction();
    SqlConnection objCon = new SqlConnection(objConStr.GetConnStr);
    SqlCommand objCmd = new SqlCommand(Query, objCon);

    SqlDataAdapter objDA = new SqlDataAdapter(objCmd);// = new SqlDataAdapter(Query, objCon, objConStr.GetConnStr);

    objDA.SelectCommand.CommandText = objCmd.CommandText.ToString();
    DataTable dt = new DataTable("Table");
    try
    {
    objDA.Fill(dt);
    }
    catch (Exception ex)
    {

    }
    return dt;

    }



Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative