Mohammed Faruk
Mohammed Faruk

Reputation: 495

How do I print Crystal Reports?

I am new comer in Crystal report printing. Any one suggest me best way of Crystal report print in C# Desktop Application With Example(Source code).

Upvotes: 1

Views: 8789

Answers (3)

Chamila Maddumage
Chamila Maddumage

Reputation: 3866

This one works for me.

private void PrintForm_Load(object sender, EventArgs e)
{
    ReportDocument cry = new ReportDocument();
    try
    {
        cry.Load(@"C:\Reports\CRYPT.rpt");//your report path
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=POS;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter("Your Stored Procedure", con);
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        sda.SelectCommand.Parameters.Add("@Doc_No", System.Data.SqlDbType.VarChar, 50).Value = Globlevariable.PrintDocNo;
        DataSet st = new DataSet();
        sda.Fill(st, "DATA");
        cry.SetDataSource(st);
            cry.PrintToPrinter(1,false,0,0);
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

Upvotes: 0

Dylan - INNO Software
Dylan - INNO Software

Reputation: 1112

Do you want users to just print when they want to?

If so, just enable the print option on the CrystalReportViewer control.

Otherwise the answer provided by kashif is the programmatic way.

Upvotes: 0

kashif
kashif

Reputation: 3834

Try this

private void button1_Click(object sender, EventArgs e)
{
    CrystalReport1 rpt = new CrystalReport1();
    SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=acc;uid=sa;pwd=fantastic;");
    cn.Open();
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable dt = new DataTable();
    cmd.Connection = cn;
    cmd.CommandText = "select EmpNo, EName from Emp";
    da.SelectCommand = cmd;
    dt.Clear();
    da.Fill(dt);
    rpt.SetDataSource(dt);
    rpt.PrintToPrinter(1, false, 0, 0);
}

Upvotes: 4

Related Questions