报表rdl嵌入网页(ASP.NET)

1      不含参数

2      含参数

3      常見問題

3.1       報表增加字段rdlc咋辦?

3.2       老是提示:尚未提供資料來源 'titles' 的資料來源執行個體。咋解決?   

3.3       提示參數:'pubId' 參數遺漏值

 

 

2011127日星期三 上午118

 

1            不含参数

假定报表已经开发好,报表服务器上的报表为:publishers.rdlTitlesByPublisher.rdl

1 publishers.rdl的扩展名为rdlc,即publishers.rdlc。其它不做任何修改。

2 在已有的专案中,建立一个资料夹,存放报表,例如:Reports

image

3 publishers.rdlc添加到Reports文件夹。

4 新增一个页面用于承载报表(publishers.rdlc),例如:Publishers.aspx

5 reportviewer控件拖到页面Publishers.aspx

image

6 设置reportviewer控件的ProcessingModeLocal,表示在ClientASP.NET)上Run

image

7 写代码为报表赋数据源

1)取得数据源

         public DataSet GetPublishers()

        {

            try

            {

                using (SqlConnection conn = new SqlConnection(connectionString))

                {

                    DataSet ds = new DataSet();

                    string cmdText = "select * from publishers ";

                    SqlCommand comm = new SqlCommand(cmdText, conn);

                    SqlDataAdapter sqlAdapter = new SqlDataAdapter(comm);

                    sqlAdapter.Fill(ds, "publishers");

                    return ds;

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

2)设置报表参数——路径、数据源

        private void BindReprotData()

        {

            DataSet ds = GetPublishers();

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)

            {

                this.ReportViewer1.LocalReport.ReportPath = "Reports\\publishers.rdlc";

                this.ReportViewer1.LocalReport.DataSources.Clear();

                this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("publishers", ds.Tables[0]));

                this.ReportViewer1.DataBind();

            }

        }

8 显示报表

        private string connectionString;

        protected void Page_Load(object sender, EventArgs e)

        {

            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["pubs"].ConnectionString;

            BindReprotData();

        }

9 结果

image

注意

ReportPath:不可使用“~”。写在代码里,方便使用配置文件。

ProcessingModeLocal

2            含参数

大体思路是:将参数写到SQL里,从DB中获取所需数据,报表参数删掉。

1 publishers.rdl的扩展名为rdlc,即publishers.rdlc。其它不做任何修改。

2 publishers.rdlc添加到項目中。

3 刪除報表中的參數。

image

4 確定,以保存。

5 添加TitlesByPublisher.aspx

6 reportviewer控件拖到页面TitlesByPublisher.aspx

7设置reportviewer控件的ProcessingModeLocal

8 在页面TitlesByPublisher.aspx上,添加一個和文本框和按鈕。ID分別為txtpub_idbtnShow

9 獲取報表數據

1

        private string connectionString;

        protected void Page_Load(object sender, EventArgs e)

        {

            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["pubs"].ConnectionString;

           

        }

        public DataSet GetTitlesByPublisher(int pubId)

        {

            try

            {

                using (SqlConnection conn = new SqlConnection(connectionString))

                {

                    DataSet ds = new DataSet();

                    string cmdText = "select * from titles where pub_id=@pubId ";

                    SqlCommand comm = new SqlCommand(cmdText, conn);

                    comm.Parameters.AddWithValue("@pubId", pubId);

                    SqlDataAdapter sqlAdapter = new SqlDataAdapter(comm);

                    sqlAdapter.Fill(ds, "Titles");

                    return ds;

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

2)設置報表屬性

        private void BindReprotData()

        {

            int pub_id;           

            if (int.TryParse(this.txtpub_id.Text.Trim(), out pub_id))

            {

                DataSet ds = GetTitlesByPublisher(pub_id);

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)

                {

                    this.ReportViewer1.LocalReport.ReportPath = @"Reports\TitlesByPublisher.rdlc";

                    this.ReportViewer1.LocalReport.DataSources.Clear();

                    this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("titles", ds.Tables[0]));

                    this.ReportViewer1.DataBind();

                }

            }

        }

10 顯示報表

        protected void btnShow_Click(object sender, EventArgs e)

        {

            BindReprotData();

        }

注意

設報表參數使用Microsoft.Reporting.WebForms.ReportParameter。實踐證明用此性能低。

3            常見問題

3.1  報表增加字段rdlc咋辦?

1 VS工具打開報表項目。

2 修改報表,添加字段。

3 修改擴展名,rdl->rdlc

3.2  老是提示:尚未提供資料來源 'titles' 的資料來源執行個體。咋解決?

1 查看報表的數據源名字。

image

image

2 修改代碼中,數據源的名字。

this.ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("titles", ds.Tables[0]))

3.3  提示參數:'pubId' 參數遺漏值

原因:報表參數沒賦值

1 定義報表參數集合

Dim params(1) As Microsoft.Reporting.WebForms.ReportParameter

2 設置每一個參數

Dim p0 As Microsoft.Reporting.WebForms.ReportParameter

'參數的名稱應和RDL報表中的參數名稱一致

p0 = New Microsoft.Reporting.WebForms.ReportParameter("pubId", pubId.Text)

params(0) = p0

3 參數集合添加到報表中

ReportViewer1.LocalReport.SetParameters(params)

 

posted @ 2012-02-13 20:31  yellowwood  阅读(3327)  评论(1编辑  收藏  举报
Never Give UP