I have a Asp.net Form with Crystal Report Viewer Control on It.
On Page_Load Event, I am loading a report & setting a DataSource To it from Database.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt= Custom.StaticGeneral.GetDataTable("Select top 100 * From CustVend order by Code desc");
dt.TableName = "CustVend";
dt.AcceptChanges();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.AcceptChanges();
ReportDocument myReportDocument;
myReportDocument = new ReportDocument();
myReportDocument.Load(Server.MapPath("CrystalReport1.rpt"));
myReportDocument.SetDataSource(ds);
CrystalReportViewer1.ReportSource = myReportDocument;
//CrystalReportViewer1.DataBind(); Also Tried This
CrystalReportViewer1.DisplayToolbar = true;
}
}
this is the relevant html
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server"
GroupTreeImagesFolderUrl="" Height="1202px"
ReuseParameterValuesOnRefresh="True" EnableDatabaseLogonPrompt="false"
ToolbarImagesFolderUrl="" ToolPanelWidth="200px" Width="1104px" />
I have set EnableDatabaseLogonPrompt="false" because I am providing my own data source & question of log in does not arise.
I get to see the Report when Page is Loaded with my data. However, When I Select "Go To Next Page" Link of the crystal report Viewer Control, An Error Pops Up as
Error : "Database logon failed."
Seems that I am missing out something (probably rebinding the dataset/Report) but can't figure it out...
I can solve this error by commenting out the Line if (!IsPostBack). but is it the right way to query the Database again & load the whole report every time user is changing a page ?
Or are there Simpler/easier options available..
The code that loads the report must be execute on every postback. Page_Init is the right place where you can put this code (Page_Load can cause some errors).
Hey, it's reccomended to close ReportDocument on every page unload; this avoid an uncontrolled increase on a Report Counter that will stop the application
protected void Page_Unload(object sender, EventArgs e)
{
if (reportDocument != null)
reportDocument.Close();
}
Put the code in LoadData method that should be called if(!IsPostBack). IsPostBack is used for Ajax calls and it's not the case for CrystalReports.
Here is how I do it:
<CR:CrystalReportSource ID="CrystalReportSource1" runat="server">
<Report FileName="myCristalReport.rpt"></Report>
</CR:CrystalReportSource>
<CR:CrystalReportViewer
ID="CrystalReportViewer1"
runat="server"
AutoDataBind="true"
EnableDatabaseLogonPrompt="False"
EnableParameterPrompt="False"
ReportSourceID="CrystalReportSource1"
ToolPanelView="None" />
and in LoadData :
var reportSource = (CrystalReportSource)this.FindControlRecursively("CrystalReportSource1");
reportSource.ReportDocument.SetDatabaseLogon("dbuser", "dbpassword");
reportSource.ReportDocument.SetParameterValue("Month", 2);
....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.