Monday, 21 July 2014

Crystal Report to print multiple Data

No comments
1 > create dataset and add columns
2>create new crystal report
3>add that dataset and design crystal report
4>

using Microsoft.Reporting.WebForms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

 private CrystalDecisions.CrystalReports.Engine.ReportDocument cr = new ReportDocument();
 static string Crypath = "";

protected void btnprine_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable d1 = new DataTable();
            d1.TableName = "t1";
            d1.Columns.Add("Email");
            d1.Columns.Add("Address");
         
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox ck = (CheckBox)GridView1.Rows[i].FindControl("print");
                if (ck.Checked == true)
                {
                    Label id = (Label)GridView1.Rows[i].FindControl("Label5");
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Example"].ToString());
                    SqlCommand cmd = new SqlCommand("SP", con);
                    cmd.Parameters.AddWithValue("@id", id.Text);
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    DataRow drow = ds.Tables[0].Rows[0];
                    DataRow dr1 = d1.NewRow();                 
                    dr1["Email"] = drow["Email"];
                    dr1["Address"] = drow["Address"];                  
                    d1.Rows.Add(dr1);
                    Crypath = Server.MapPath("~/CrystalReport.rpt").ToString();
                    cr.Load(Crypath);
                    cr.Database.Tables[0].SetDataSource(d1);
                    cr.Refresh();
                }
            }
            Cry("_Report");
        }
        catch(Exception)
        {
           
        }
    }
    public void Cry(string titlename)
    {
        cr.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, titlename);
    }


Another Way

In Report Page

         {


        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);

        DataView dv = new DataView(ds);

        if (Txt_Search_name.Text != "")
        {
            dv.RowFilter = "name LIKE '" + Txt_Search_name.Text.TrimEnd() + "%'";
            dv.RowStateFilter = DataViewRowState.CurrentRows;
            HttpContext.Current.Session["Para1"] = Txt_Search_name.Text.TrimEnd() + "%'";
        }
        else
        {
            HttpContext.Current.Session["Para1"] = "All";
        }

        if (Txt_Search_Mobile.Text == "")
        {
            HttpContext.Current.Session["Para2"] = "All";
        }
        else
        {
            HttpContext.Current.Session["Para2"] = Txt_Search_Mobile.Text;
        }

        if (DD_KYC.SelectedIndex == 0)
        {
            HttpContext.Current.Session["Para3"] = "All";
        }       
        else
        {
            HttpContext.Current.Session["Para4"] = DD_Type.SelectedValue;
        }
        DataTable dt = new DataTable();
        dt = dv.ToTable();
        HttpContext.Current.Session["DatatableReport"] = dt;
        GridView_data.DataSource = dt;
        GridView_data.DataBind();
    }
    protected void imgpdf_Click(object sender, ImageClickEventArgs e)
    {
        Btn_Search_Click(sender, e);
        string url = "Default_report.aspx?Name=List";
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "NewWindow", "window.open('" + url + "','_blank');", true);
    }
 

Create New Default_report.aspx




if (Request.QueryString["Name"] != null)
            {
                if (Request.QueryString["Name"] == "List")
                {
                    DataTable dt1 = new DataTable();
                    dt1 = (DataTable)HttpContext.Current.Session["DatatableReport"];          
                    ReportDocument crystalReport = new ReportDocument();
                    crystalReport.Load(Server.MapPath("../Reports/xyz.rpt"));
                    crystalReport.SetDataSource(dt1);
                    crystalReport.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "_Report");
                    //CrystalReportViewer1.ReportSource = crystalReport;
                    //CrystalReportViewer1.DataBind();
                }
}



For Loading PDF In Browser

 protected void Download_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conection"].ToString());
        SqlCommand cmd = new SqlCommand("reg", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RegNo", No.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);

        ReportDocument crystalReport = new ReportDocument();

        if (lblChlnNo.Text.Contains("T"))
        {
            crystalReport.Load(Server.MapPath("../Reports/Report.rpt"));
        }
     
        crystalReport.SetDataSource(ds);
      
        try
        {
            ExportOptions CrExportOptions;
            DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
            PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
            CrDiskFileDestinationOptions.DiskFileName = Server.MapPath(@"../Reports/AllReport" + No.Text + ".pdf");
            CrExportOptions = crystalReport.ExportOptions;
            {
                CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                CrExportOptions.FormatOptions = CrFormatTypeOptions;
            }
            crystalReport.Export();
        }
        catch (Exception ex)
        {

        }

        string url = "../Stores/Default_report.aspx?Name=Rpt_No";
        HttpContext.Current.Session["url"] = "../Reports/AllReport" + No.Text + ".pdf");

        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "NewWindow", "window.open('" + url + "','_blank');", true);
        clear();

    }

 

in Default.aspx

 

 if (Request.QueryString["Name"] == "Rpt_No")
                {                 
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "NewWindow", "window.open('" + HttpContext.Current.Session["url"] + "','_self');", true);
                }