Problem: Crystal Report has issue a demonistration code to show that how to display Crystal Report embedded in ASP.NET web page. However, programmers always
confront problem that the code only display two page with the netvigator button. It cannot go beyond page if the report has.
The main problem of this issue is that we always commit the wrong concept that we place the Report loading code over the Page_Load() instead of
Page_Init()
Two Main Points to solve the problem:
1. Load crytal report at Page_Init() instead of Page_Load()
2. Use Session in Page to store the report at Server, and load it back when Page is not postback
The Correct version:
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
else
{
mb_objcryRptDoc = (ReportDocument)Session["rptDoc"]; <-------------------------------------------------------Restore the report from Session for overcome the
-------------------------------------------------------Page beyond page 3 problem.
} // end if ... else (!IsPostBack)
// ------------------------------------------------
mb_fnInitFillAll(); <------------------------------------------------------ I load crystal report at this method
}
// #############
// # Page Load #
// #############
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FS = FORM_STATE.INIT;
ViewState.Add("FS", FS);
mb_fnSetFormState(FS);
}
else
{
FS = (FORM_STATE)ViewState["FS"];
mb_fnSetFormState(FS);
} // end if ... else (!IsPostBack)
// ------------------------------------------------
// code to check user Login
mb_objUserSession = new UserSession();
if (!mb_objUserSession.pUser_Login)
{
Page.Response.Redirect("./frmUserNotPermitted.html");
} // end if (!mb_objUserSession.pUser_Login)
// code to check user permissions
mb_fnCheckPermissions();
} // end Page_Load(object sender, EventArgs e)
// #############
// #############
/* *
* method to init and fill all
* */
private void mb_fnInitFillAll()
{
mb_fnInit();
// for user authentication
mb_fnInitUserObject(); // vwrUserObject
mb_fnInitUserGroupObject(); // vwrUserGroupObject
if (Request.QueryString["Quotation_IDP"] != null && Request.QueryString["Quotation_IDN"] != null &&
Request.QueryString["Quotation_IDR"] != null)
{
txtQuotation_IDP.Text = Request.QueryString["Quotation_IDP"].ToString();
txtQuotation_IDN.Text = Request.QueryString["Quotation_IDN"].ToString();
txtQuotation_IDR.Text = Request.QueryString["Quotation_IDR"].ToString();
cbQuotation_ID.Checked = true;
cbQuotation_ID.Enabled = false;
}
string strQueryString = "";
if (Request.QueryString["URL"] != null)
{
strQueryString = Request.QueryString["URL"] + "?Quotation_IDP=" + txtQuotation_IDP.Text +
"&Quotation_IDN=" + txtQuotation_IDN.Text +
"&Quotation_IDR=" + txtQuotation_IDR.Text;
} // end if (Request.QueryString["URL"] != null)
if (Request.QueryString["URL2"] != null)
{
strQueryString += "&" + Request.QueryString["URL2"].ToString();
} // end if (Request.QueryString["URL2"] != null)
if (Request.QueryString["URL3"] != null)
{
strQueryString += "&" + Request.QueryString["URL3"].ToString();
} // end if (Request.QueryString["URL3"] != null)
this.hplBacktoQuotation.NavigateUrl=strQueryString;
if (Request.QueryString["URL"] != null)
{
if (Request.QueryString["URL"].ToString().CompareTo("frmQuotationCMgt.aspx") == 0 ||
Request.QueryString["URL"].ToString().CompareTo("frmrptQuotationC.aspx") == 0 )
{
txtQuotationType.Text = "cement";
}
else
{
txtQuotationType.Text = "tile";
}
}
else
{
txtQuotationType.Text = "tile";
}
// ConfigureCrystalReports()
ConfigureCrystalReports(); <----------------------------------------------------------------------------- I load Crystal Report here !
} // end mb_fnInitFillAll()
/* *
* Method to ConfigureCrystal Reports()
* */
private void ConfigureCrystalReports()
{
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = mb_objConn.pstr_ConnString.pstr_server;
connectionInfo.DatabaseName = mb_objConn.pstr_ConnString.pstr_initialCatalog;
connectionInfo.UserID = mb_objConn.pstr_ConnString.pstr_userID;
connectionInfo.Password = mb_objConn.pstr_ConnString.pstr_password;
string reportPath = Server.MapPath("./reports/rptQuotation.rpt");
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(reportPath);
cryRptVwr.ReportSource = rptDoc;
//cryRptVwr.CssFilename = "http://localhost/crystalreportviewers115/css/default.css";
//cryRptVwr.ToolbarImagesFolderUrl = "http://localhost/crystalreportviewers115/images/toolbar/";
//cryRptVwr.GroupTreeImagesFolderUrl = "http://localhost/crystalreportviewers115/images/tree/";
SetDBLogonForReport(connectionInfo, rptDoc);
// ---------------------------------------------------------------------------------------------------
StringBuilder sbConnStr = new StringBuilder();
sbConnStr.Append("Server=" + mb_objConn.pstr_ConnString.pstr_server + ";");
sbConnStr.Append("uid=" + mb_objConn.pstr_ConnString.pstr_userID + ";");
sbConnStr.Append("password=" + mb_objConn.pstr_ConnString.pstr_password + ";");
sbConnStr.Append("initial catalog=" + mb_objConn.pstr_ConnString.pstr_initialCatalog + ";");
SqlConnection sqlConn = new SqlConnection(sbConnStr.ToString());
//SqlConnection sqlConn = new SqlConnection("Server=st;uid=sa;password=adminis;initial catalog=dbMRP;");
// --------------------------------- vwrQuotation -----------------------------
SqlCommand comd;
comd = new SqlCommand();
comd.Connection = sqlConn;
comd.CommandType = CommandType.Text;
comd.CommandText = mb_fnConcatenateSelect_Quotation();
//comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
//if (TextBox1.Text.Trim() != "")
// comd.Parameters[0].Value = TextBox1.Text;
//else
// comd.Parameters[0].Value = DBNull.Value;
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = comd;
DataSet ds = new DataSet();
ds.ReadXmlSchema(Server.MapPath("./dataSet/dsQuotation.xsd"));
sqlAdapter.Fill(ds, "vwrQuotation");
//// ---------------------------------- SProductPrices ---------------------------
SqlCommand comd2;
comd2 = new SqlCommand();
comd2.Connection = sqlConn;
comd2.CommandType = CommandType.Text;
comd2.CommandText = mb_fnConcatenateSelect_QuotationDItem();
//comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
//if (TextBox1.Text.Trim() != "")
// comd.Parameters[0].Value = TextBox1.Text;
//else
// comd.Parameters[0].Value = DBNull.Value;
SqlDataAdapter sqlAdapter2 = new SqlDataAdapter();
sqlAdapter2.SelectCommand = comd2;
sqlAdapter2.Fill(ds, "vwrQuotationDItem");
//// --------------------------------------------------------------------------------
//// ---------------------------------- SProductPrices ---------------------------
//SqlCommand comd3;
//comd3 = new SqlCommand();
//comd3.Connection = sqlConn;
//comd3.CommandType = CommandType.Text;
//comd3.CommandText = mb_fnConcatenateSelect_SProductPrices();
////comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
////if (TextBox1.Text.Trim() != "")
//// comd.Parameters[0].Value = TextBox1.Text;
////else
//// comd.Parameters[0].Value = DBNull.Value;
//SqlDataAdapter sqlAdapter3 = new SqlDataAdapter();
//sqlAdapter3.SelectCommand = comd3;
//sqlAdapter3.Fill(ds, "vwrSProductPrices");
//// --------------------------------------------------------------------------------
//rptDoc.Database.Tables["vwrQuotationD"].ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSProductPrices"].ApplyLogOnInfo(tableLogOnInfo);
cryRptVwr.ReportSource = rptDoc;
rptDoc.PrintOptions.PrinterName = "Epson LQ-2170 ESC/P 2";
System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
printDocument.PrinterSettings.PrinterName = "Epson LQ-2170 ESC/P 2";
for (int i = 0; i < printDocument.PrinterSettings.PaperSizes.Count; i++)
{
int rawKind;
if (printDocument.PrinterSettings.PaperSizes.PaperName == "Fanfold 210 x 305 mm")
{
//rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize) printDocument.PrinterSettings.PaperSizes.RawKind;
rawKind = Convert.ToInt32(printDocument.PrinterSettings.PaperSizes.GetType().GetField("kind", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(printDocument.PrinterSettings.PaperSizes));
//crPrintOut.PrintOptions.PaperSize = rawKind;
rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)rawKind;
}
}
cryRptVwr.PrintMode = CrystalDecisions.Web.PrintMode.Pdf;
rptDoc.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
rptDoc.PrintOptions.PaperSource = PaperSource.Tractor;
cryRptVwr.EnableParameterPrompt = false;
// - SubReport rptProductionBatchD
ReportDocument rptDocSub = rptDoc.Subreports[0];
rptDocSub.SetDataSource(ds);
//string strSubReportName = rptDoc.Subreports[0].Name;
SetDBLogonForReport(connectionInfo, rptDoc);
SetDBLogonForReport(connectionInfo, rptDocSub);
//SetDBLogonForReport(connectionInfo, rptDocSub2);
// - end Sub Report rptProductionBatchD
cryRptVwr.ReportSource = rptDoc;
cryRptVwr.Visible = true;
mb_objcryRptDoc = rptDoc; // for handling the problem with crystal report deficit.
// crystal report 11 The maximum report processing jobs limit configured by your
// system administrator has been reached.
//rptDoc.PrintToPrinter(1, true, 0, 0);
Session["rptDoc"] = mb_objcryRptDoc; <--------------------------------------------------------------------Remember to store the report at Session
} // end ConfigureCrystalReports
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument _rptDoc)
{
TableLogOnInfos tableLogOnInfos =
cryRptVwr.LogOnInfo;
foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
{
tableLogOnInfo.ConnectionInfo = connectionInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in _rptDoc.Database.Tables)
{
table.ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSupplierSProductSProductTypeAll_NoPrice"].ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSProductPrices"].ApplyLogOnInfo(tableLogOnInfo);
}
}
} // end SetDBLogonForReport(connectionInfo connectionInfo)
Problem Solved:
================================================================================================================================
The following is the code that it doesn’t overcome the problem:
// #############
// # Page Load #
// #############
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FS = FORM_STATE.INIT;
ViewState.Add("FS", FS);
mb_fnSetFormState(FS);
//Session["rptDoc"] = mb_objcryRptDoc;
//// Restore Previous tbContainer.ActiveTab
//if (Request.QueryString["tbContainer_ActiveTab"] != null)
//{
// string str_tbContainer = Request.QueryString["tbContainer_ActiveTab"].ToString();
// switch (str_tbContainer)
// {
// case "tpPR":
// tbContainer.ActiveTab = tpPR;
// break;
// case "tpPRD":
// tbContainer.ActiveTab = tpPRD;
// break;
// default:
// tbContainer.ActiveTab = tpPR;
// break;
// } // end switch (str_tbContainer)
//} // end if (Request.QueryString["tbContainer_ActiveTab"] != null)
}
else
{
FS = (FORM_STATE)ViewState["FS"];
mb_fnSetFormState(FS);
//mb_objcryRptDoc = (ReportDocument)Session["rptDoc"];
} // end if ... else (!IsPostBack)
// ------------------------------------------------
// code to check user Login
mb_objUserSession = new UserSession();
if (!mb_objUserSession.pUser_Login)
{
Page.Response.Redirect("./frmUserNotPermitted.html");
} // end if (!mb_objUserSession.pUser_Login)
//// code to initial and bind all
mb_fnInitFillAll(); <--------------------------------------------before I put Load Crystal Report function here
// code to check user permissions
mb_fnCheckPermissions();
} // end Page_Load(object sender, EventArgs e)
/* *
* method to init and fill all
* */
private void mb_fnInitFillAll()
{
mb_fnInit();
// for user authentication
mb_fnInitUserObject(); // vwrUserObject
mb_fnInitUserGroupObject(); // vwrUserGroupObject
if (Request.QueryString["Quotation_IDP"] != null && Request.QueryString["Quotation_IDN"] != null &&
Request.QueryString["Quotation_IDR"] != null)
{
txtQuotation_IDP.Text = Request.QueryString["Quotation_IDP"].ToString();
txtQuotation_IDN.Text = Request.QueryString["Quotation_IDN"].ToString();
txtQuotation_IDR.Text = Request.QueryString["Quotation_IDR"].ToString();
cbQuotation_ID.Checked = true;
cbQuotation_ID.Enabled = false;
}
string strQueryString = "";
if (Request.QueryString["URL"] != null)
{
strQueryString = Request.QueryString["URL"] + "?Quotation_IDP=" + txtQuotation_IDP.Text +
"&Quotation_IDN=" + txtQuotation_IDN.Text +
"&Quotation_IDR=" + txtQuotation_IDR.Text;
} // end if (Request.QueryString["URL"] != null)
if (Request.QueryString["URL2"] != null)
{
strQueryString += "&" + Request.QueryString["URL2"].ToString();
} // end if (Request.QueryString["URL2"] != null)
if (Request.QueryString["URL3"] != null)
{
strQueryString += "&" + Request.QueryString["URL3"].ToString();
} // end if (Request.QueryString["URL3"] != null)
this.hplBacktoQuotation.NavigateUrl=strQueryString;
if (Request.QueryString["URL"] != null)
{
if (Request.QueryString["URL"].ToString().CompareTo("frmQuotationCMgt.aspx") == 0 ||
Request.QueryString["URL"].ToString().CompareTo("frmrptQuotationC.aspx") == 0 )
{
txtQuotationType.Text = "cement";
}
else
{
txtQuotationType.Text = "tile";
}
}
else
{
txtQuotationType.Text = "tile";
}
ConfigureCrystalReports(); <--------------------------------------------------------- it appears here
} // end mb_fnInitFillAll()
/* *
* Method to ConfigureCrystal Reports()
* */
private void ConfigureCrystalReports()
{
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = mb_objConn.pstr_ConnString.pstr_server;
connectionInfo.DatabaseName = mb_objConn.pstr_ConnString.pstr_initialCatalog;
connectionInfo.UserID = mb_objConn.pstr_ConnString.pstr_userID;
connectionInfo.Password = mb_objConn.pstr_ConnString.pstr_password;
string reportPath = Server.MapPath("./reports/rptQuotation.rpt");
ReportDocument rptDoc = new ReportDocument();
rptDoc.Load(reportPath);
cryRptVwr.ReportSource = rptDoc;
//cryRptVwr.CssFilename = "http://localhost/crystalreportviewers115/css/default.css";
//cryRptVwr.ToolbarImagesFolderUrl = "http://localhost/crystalreportviewers115/images/toolbar/";
//cryRptVwr.GroupTreeImagesFolderUrl = "http://localhost/crystalreportviewers115/images/tree/";
SetDBLogonForReport(connectionInfo, rptDoc);
// ---------------------------------------------------------------------------------------------------
StringBuilder sbConnStr = new StringBuilder();
sbConnStr.Append("Server=" + mb_objConn.pstr_ConnString.pstr_server + ";");
sbConnStr.Append("uid=" + mb_objConn.pstr_ConnString.pstr_userID + ";");
sbConnStr.Append("password=" + mb_objConn.pstr_ConnString.pstr_password + ";");
sbConnStr.Append("initial catalog=" + mb_objConn.pstr_ConnString.pstr_initialCatalog + ";");
SqlConnection sqlConn = new SqlConnection(sbConnStr.ToString());
//SqlConnection sqlConn = new SqlConnection("Server=st;uid=sa;password=adminis;initial catalog=dbMRP;");
// --------------------------------- vwrQuotation -----------------------------
SqlCommand comd;
comd = new SqlCommand();
comd.Connection = sqlConn;
comd.CommandType = CommandType.Text;
comd.CommandText = mb_fnConcatenateSelect_Quotation();
//comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
//if (TextBox1.Text.Trim() != "")
// comd.Parameters[0].Value = TextBox1.Text;
//else
// comd.Parameters[0].Value = DBNull.Value;
SqlDataAdapter sqlAdapter = new SqlDataAdapter();
sqlAdapter.SelectCommand = comd;
DataSet ds = new DataSet();
ds.ReadXmlSchema(Server.MapPath("./dataSet/dsQuotation.xsd"));
sqlAdapter.Fill(ds, "vwrQuotation");
//// ---------------------------------- SProductPrices ---------------------------
SqlCommand comd2;
comd2 = new SqlCommand();
comd2.Connection = sqlConn;
comd2.CommandType = CommandType.Text;
comd2.CommandText = mb_fnConcatenateSelect_QuotationDItem();
//comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
//if (TextBox1.Text.Trim() != "")
// comd.Parameters[0].Value = TextBox1.Text;
//else
// comd.Parameters[0].Value = DBNull.Value;
SqlDataAdapter sqlAdapter2 = new SqlDataAdapter();
sqlAdapter2.SelectCommand = comd2;
sqlAdapter2.Fill(ds, "vwrQuotationDItem");
//// --------------------------------------------------------------------------------
//// ---------------------------------- SProductPrices ---------------------------
//SqlCommand comd3;
//comd3 = new SqlCommand();
//comd3.Connection = sqlConn;
//comd3.CommandType = CommandType.Text;
//comd3.CommandText = mb_fnConcatenateSelect_SProductPrices();
////comd.Parameters.Add("@Companyname", SqlDbType.VarChar, 50);
////if (TextBox1.Text.Trim() != "")
//// comd.Parameters[0].Value = TextBox1.Text;
////else
//// comd.Parameters[0].Value = DBNull.Value;
//SqlDataAdapter sqlAdapter3 = new SqlDataAdapter();
//sqlAdapter3.SelectCommand = comd3;
//sqlAdapter3.Fill(ds, "vwrSProductPrices");
//// --------------------------------------------------------------------------------
//rptDoc.Database.Tables["vwrQuotationD"].ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSProductPrices"].ApplyLogOnInfo(tableLogOnInfo);
cryRptVwr.ReportSource = rptDoc;
rptDoc.PrintOptions.PrinterName = "Epson LQ-2170 ESC/P 2";
System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
printDocument.PrinterSettings.PrinterName = "Epson LQ-2170 ESC/P 2";
for (int i = 0; i < printDocument.PrinterSettings.PaperSizes.Count; i++)
{
int rawKind;
if (printDocument.PrinterSettings.PaperSizes.PaperName == "Fanfold 210 x 305 mm")
{
//rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize) printDocument.PrinterSettings.PaperSizes.RawKind;
rawKind = Convert.ToInt32(printDocument.PrinterSettings.PaperSizes.GetType().GetField("kind", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(printDocument.PrinterSettings.PaperSizes));
//crPrintOut.PrintOptions.PaperSize = rawKind;
rptDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)rawKind;
}
}
cryRptVwr.PrintMode = CrystalDecisions.Web.PrintMode.Pdf;
rptDoc.PrintOptions.PaperOrientation = PaperOrientation.Portrait;
rptDoc.PrintOptions.PaperSource = PaperSource.Tractor;
cryRptVwr.EnableParameterPrompt = false;
// - SubReport rptProductionBatchD
ReportDocument rptDocSub = rptDoc.Subreports[0];
rptDocSub.SetDataSource(ds);
//string strSubReportName = rptDoc.Subreports[0].Name;
SetDBLogonForReport(connectionInfo, rptDoc);
SetDBLogonForReport(connectionInfo, rptDocSub);
//SetDBLogonForReport(connectionInfo, rptDocSub2);
// - end Sub Report rptProductionBatchD
cryRptVwr.ReportSource = rptDoc;
cryRptVwr.Visible = true;
mb_objcryRptDoc = rptDoc; // for handling the problem with crystal report deficit.
// crystal report 11 The maximum report processing jobs limit configured by your
// system administrator has been reached.
//rptDoc.PrintToPrinter(1, true, 0, 0);
Session["rptDoc"] = mb_objcryRptDoc;
} // end ConfigureCrystalReports
private void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument _rptDoc)
{
TableLogOnInfos tableLogOnInfos =
cryRptVwr.LogOnInfo;
foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
{
tableLogOnInfo.ConnectionInfo = connectionInfo;
foreach (CrystalDecisions.CrystalReports.Engine.Table table in _rptDoc.Database.Tables)
{
table.ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSupplierSProductSProductTypeAll_NoPrice"].ApplyLogOnInfo(tableLogOnInfo);
//rptDoc.Database.Tables["vwrSProductPrices"].ApplyLogOnInfo(tableLogOnInfo);
}
}
} // end SetDBLogonForReport(connectionInfo connectionInfo)
manpakhong