i used your provided approach to print direct crystal report without preview
it bind on view button and there is an error
Error: 'bobj' is undefined
on internet explorer
my code below
Html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Petrol.WebForm2" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=13.0.3500.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script type="text/javascript">
function Print(){
var dvReport = document.getElementById("dvReport");
var frame1 = dvReport.getElementsByTagName("iframe")[0];
if (navigator.appName.indexOf("Internet Explorer") != -1) {
frame1.name = frame1.id;
window.frames[frame1.id].focus();
window.frames[frame1.id].print();
}
else {
var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
frameDoc.print();
}
}
</script>
<body>
<form id="form1" runat="server">
<div id="dvReport">
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true"
Height="400" Width="600" BestFitPage="False" />
</div>
<asp:TextBox ID="txtstart" runat="server" TextMode="Date"></asp:TextBox>
<asp:TextBox ID="txtend" runat="server" TextMode="Date"></asp:TextBox>
<asp:TextBox ID="TextBox1" runat="server" TextMode="Date"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Date"></asp:TextBox>
<asp:DropDownList ID="ddlcus" Height="46px" Width="150px" CssClass="form-control js-example-placeholder-single"
runat="server" ToolTip="Select ">
</asp:DropDownList>
<asp:Button ID="View" runat="server" Text="View" OnClick="View_Click" Width="53px" />
<input type="button" id="btnPrint" value="Print" onclick="Print()" />
</form>
</body>
</html>
C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
namespace Petrol
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=PPMS;MultipleActiveResultSets=True;");
ReportDocument rpt = new ReportDocument();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// loadvehicle();
loadcustomer();
}
}
private void loadcustomer()
{
con.Open();
SqlDataAdapter adpbp = new SqlDataAdapter("select * from tbl_Customer ", con);
DataSet dsbp = new DataSet();
adpbp.Fill(dsbp);
ddlcus.DataSource = dsbp.Tables[0];
ddlcus.DataTextField = "C_Name";
ddlcus.DataValueField = "C_ID";
ddlcus.DataBind();
ddlcus.Items.Insert(0, new ListItem("Select Customer ", "0"));
con.Close();
}
protected void View_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SERVER1\\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=PPMS;MultipleActiveResultSets=True;");
con.Open();
SqlCommand cmd = new SqlCommand("Sp_Allslps_cus", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Customer", ddlcus.SelectedValue);
cmd.Parameters.Add(new SqlParameter("@StartDate", DateTime.Parse(txtstart.Text).ToString("M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)));
cmd.Parameters.Add(new SqlParameter("@EndDate", DateTime.Parse(txtend.Text).ToString("M/d/yyyy ", System.Globalization.CultureInfo.InvariantCulture)));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
rpt = new ReportDocument();
rpt.Load(Server.MapPath("~/cusslp.rpt"));
rpt.SetDataSource(ds.Tables["Table"]);
CrystalReportViewer1.ReportSource = rpt;
CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;
}
}
}