using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Xml;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Reporting;
using CrystalDecisions.Shared;
using CrystalDecisions.Web;
public partial class Schools : System.Web.UI.Page
{
ReportDocument crystalReport = new ReportDocument();
protected void Page_Init(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
string query = "select * from School Where Designation = 'Public' Order by School ASC";
crystalReport.Load(Server.MapPath("ReportPrimary.rpt"));
MPrimary dsCustomers = GetData(query);
crystalReport.SetDataSource(dsCustomers);
crystalReport.SetDatabaseLogon("username", "password.34", "mssql.webaddress.net,4118", "dataDB");
SchReport.ReportSource = crystalReport;
Session["ReportDocument"] = crystalReport;
}
catch (LogOnException ex)
{
//Throws an exception if an error is encountered on retrieval
}
}
else
{
string query = "select * from School Where Designation = " + "'" + drpSchoolDesignation.SelectedValue + "'" + " Order by School ASC";
// ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("ReportPrimary.rpt"));
TextObject to = (TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["Text3"];
to.Text = drpSchoolDesignation.SelectedValue.ToUpper() + " SCHOOLS";
MPrimary dsCustomers = GetData(query);
crystalReport.SetDataSource(dsCustomers);
SchReport.FindControl("Text3");
crystalReport.SetDatabaseLogon("username", "password.34", "mssql.webaddress.net,4118", "dataDB");
ReportDocument doc = (ReportDocument)Session["ReportDocument"];
SchReport.ReportSource = doc;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void OpenNewWindow(string url, string windowName, string windowParams)
{
if (url == null)
throw new ArgumentNullException("url");
if (windowName == null)
windowName = "";
if (windowParams == null)
windowParams = "";
string scriptCode =
string.Format(
"<script>window.open('{0}','{1}','{2}');</script>",
url, windowName, windowParams);
//write the script out to HTTP Response stream
Response.Write(scriptCode);
}
private MPrimary GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["DBConnectionString1"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (MPrimary dsCustomers = new MPrimary())
{
sda.Fill(dsCustomers, "School");
return dsCustomers;
}
}
}
}
protected void drpSchoolDesignation_SelectedIndexChanged(object sender, EventArgs e)
{
drpDisplay.SelectedIndex = 0;
drpSort.SelectedIndex = 0;
string query = "select * from School Where Designation = " + "'" + drpSchoolDesignation.SelectedValue + "'" + " Order by School ASC";
crystalReport.Load(Server.MapPath("ReportPrimary.rpt"));
TextObject to = (TextObject)crystalReport.ReportDefinition.Sections["Section2"].ReportObjects["Text3"];
to.Text = drpSchoolDesignation.SelectedValue.ToUpper() + " SCHOOLS";
MPrimary dsCustomers = GetData(query);
crystalReport.SetDataSource(dsCustomers);
crystalReport.SetDatabaseLogon("username", "password.34", "mssql.webaddress.net,4118", "dataDB");
Session["ReportDocument"] = crystalReport;
SchReport.ReportSource = crystalReport;
}
protected void CrystalReportViewer1_Unload(object sender, EventArgs e)
{
crystalReport.Close();
crystalReport.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
//Select data from DB
//Action to be added later
}
}
此代码应该使用水晶报告检索和显示信息。 看来我的对象没有得到妥善处理。修改此代码会禁止用户访问文档的第3页。任何帮助,将不胜感激。提前谢谢你们。