我找了段水晶报表的代码,改了下,有几句话没看懂,然后页面显示加载报表失败。请高手赐教代码: protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
initControl();
loadReport();
}
paperSourceList.DataSource = GetPaperSources();
DataBind(); }
//纸张资源列表
private ArrayList GetPaperSources()
{
ArrayList arrayList = new ArrayList();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = this.thePrinter.SelectedValue;
foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
{
arrayList.Add(paperSource.SourceName.ToString());
}
return arrayList;
}
//获得打印机列表
private ArrayList GetPrinter()
{
ArrayList array = new ArrayList();
foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
array.Add(iprt);
return array;
}
//设置打印选项
private void SetPrintOptions()
{
PrintOptions printOptions = this.CrystalReportSource1.ReportDocument.PrintOptions;
printOptions.PrinterName = this.thePrinter.SelectedValue;
printOptions.PaperOrientation = (PaperOrientation)paperOrientationList.SelectedIndex;
printOptions.PaperSize = (PaperSize)paperSizeList.SelectedIndex;
printOptions.PrinterDuplex = (PrinterDuplex)printerDuplexList.SelectedIndex;
printOptions.CustomPaperSource = GetSelectedPaperSource();
}
//实例化下拉列表
private void initControl()
{
paperOrientationList.DataSource = System.Enum.GetValues(typeof(PaperOrientation));
paperSizeList.DataSource = System.Enum.GetValues(typeof(PaperSize));
printerDuplexList.DataSource = System.Enum.GetValues(typeof(PrinterDuplex));
thePrinter.DataSource = GetPrinter();
paperSourceList.DataSource = GetPaperSources();
DataBind();
}
//获得选定的打印机资源
private System.Drawing.Printing.PaperSource GetSelectedPaperSource()
{
System.Drawing.Printing.PaperSource selectedPaperSource = new System.Drawing.Printing.PaperSource();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = this.thePrinter.SelectedValue;
foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
{
if (paperSource.SourceName == paperSourceList.SelectedItem.Text)
{
selectedPaperSource = paperSource;
}
}
return selectedPaperSource;
}
// 加载报表
private void loadReport()
{
try
{
Common.Data.IDataProvider myProvider = Common.Frame.DataOper.GetSystemDataProvider();
string sql = "Select * from gongjiao_daiban";
DataTable dt = myProvider.GetData(sql);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
CrystalReportSource1.ReportDocument.Load(Server.MapPath("printUser.rpt"));
//这句不太明白
CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["gongjiao_daiban"]);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
//hierarchicalGroupingReport = this.CrystalReportSource1.ReportDocument;
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
//打印
protected void printReport_Click(object sender, EventArgs e)
{
SetPrintOptions();
try
{
this.CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 1, 99);
message.Text = "success";
}
catch (Exception aaa)
{
message.Text = aaa.Message;
//这两个message我也不知 该在哪定义下 ,我知道这个是错误信息
}
}
{
if (!IsPostBack)
{
initControl();
loadReport();
}
paperSourceList.DataSource = GetPaperSources();
DataBind(); }
//纸张资源列表
private ArrayList GetPaperSources()
{
ArrayList arrayList = new ArrayList();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = this.thePrinter.SelectedValue;
foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
{
arrayList.Add(paperSource.SourceName.ToString());
}
return arrayList;
}
//获得打印机列表
private ArrayList GetPrinter()
{
ArrayList array = new ArrayList();
foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
array.Add(iprt);
return array;
}
//设置打印选项
private void SetPrintOptions()
{
PrintOptions printOptions = this.CrystalReportSource1.ReportDocument.PrintOptions;
printOptions.PrinterName = this.thePrinter.SelectedValue;
printOptions.PaperOrientation = (PaperOrientation)paperOrientationList.SelectedIndex;
printOptions.PaperSize = (PaperSize)paperSizeList.SelectedIndex;
printOptions.PrinterDuplex = (PrinterDuplex)printerDuplexList.SelectedIndex;
printOptions.CustomPaperSource = GetSelectedPaperSource();
}
//实例化下拉列表
private void initControl()
{
paperOrientationList.DataSource = System.Enum.GetValues(typeof(PaperOrientation));
paperSizeList.DataSource = System.Enum.GetValues(typeof(PaperSize));
printerDuplexList.DataSource = System.Enum.GetValues(typeof(PrinterDuplex));
thePrinter.DataSource = GetPrinter();
paperSourceList.DataSource = GetPaperSources();
DataBind();
}
//获得选定的打印机资源
private System.Drawing.Printing.PaperSource GetSelectedPaperSource()
{
System.Drawing.Printing.PaperSource selectedPaperSource = new System.Drawing.Printing.PaperSource();
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = this.thePrinter.SelectedValue;
foreach (System.Drawing.Printing.PaperSource paperSource in printerSettings.PaperSources)
{
if (paperSource.SourceName == paperSourceList.SelectedItem.Text)
{
selectedPaperSource = paperSource;
}
}
return selectedPaperSource;
}
// 加载报表
private void loadReport()
{
try
{
Common.Data.IDataProvider myProvider = Common.Frame.DataOper.GetSystemDataProvider();
string sql = "Select * from gongjiao_daiban";
DataTable dt = myProvider.GetData(sql);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
CrystalReportSource1.ReportDocument.Load(Server.MapPath("printUser.rpt"));
//这句不太明白
CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["gongjiao_daiban"]);
CrystalReportSource1.DataBind();
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
//hierarchicalGroupingReport = this.CrystalReportSource1.ReportDocument;
}
catch (Exception exc)
{
Response.Write(exc.Message);
}
}
//打印
protected void printReport_Click(object sender, EventArgs e)
{
SetPrintOptions();
try
{
this.CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 1, 99);
message.Text = "success";
}
catch (Exception aaa)
{
message.Text = aaa.Message;
//这两个message我也不知 该在哪定义下 ,我知道这个是错误信息
}
}
//加载你的报名。Server.MapPath是你的项目所在的路径,
//Server.MapPath("printUser.rpt"))则是你报表的路径
//Message应该是实例化的一个对象或是静态对象,Text是它的属性。
//在你的类里应该还有一个显示消息提示的方法
//加载报表失败可以看下你的路径是否正确
你没有给这个Table命名呀。
你用ds.Tables[0]试试!
DataTable dt = myProvider.GetData(sql);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
不用给table命名阿,不过我试了,你说的方法ds.Tables[0],还是不行。这句话直接被捕获了。CrystalReportSource1.ReportDocument.SetDataSource()这个方法里面可以给datatable,我也试了。还是不行- -!
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Runtime.InteropServices.COMException: 拒绝访问。源错误:
行 112: //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
行 113: //ds.Tables["gongjiao_daiban"]
行 114: CrystalReportSource1.ReportDocument.SetDataSource(dt);行 115: //{?}中的参数可以不用赋值,即使赋了值也不起作用。
行 116:
http://topic.csdn.net/u/20090626/18/0e7d0af0-2555-479d-9cb5-84175787f2cd.html?80826