当前位置:编程学习 > C#/ASP.NET >>

Dispose 如何实现水晶报表关闭

阿泰,各位高手,帮我看一下,我使用水晶报表进行查询代码如下:
 public static ReportDocument myReport;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        this.Get();
    }
    private void Get()
    {

        string cn = System.Configuration.ConfigurationManager.ConnectionStrings["wce"].ConnectionString;
        SqlConnection conn = new SqlConnection(cn);
        SqlCommand sda = new SqlCommand();
        sda.Connection = conn;
        conn.Open();
        string sql = "zz_test";
        SqlCommand s = new SqlCommand(sql, conn);
        s.CommandType = CommandType.StoredProcedure;
        s.Parameters.Add("@GCRCLMLH", SqlDbType.VarChar);
        s.Parameters["@GCRCLMLH"].Value = 制令号.Text;
        DataSet1 dt = new DataSet1();
        SqlDataAdapter ds = new SqlDataAdapter(s);
        ds.Fill(dt, "zongzhuang");
        myReport = new ReportDocument();
        string reportPath = Server.MapPath("~/zz.rpt");
        myReport.Load(reportPath);
        myReport.SetDataSource(dt);
        Session["myRpt_zz"] = myReport;
         myReport.Dispose();
        CrystalReportViewer1.ReportSource = (ReportDocument)Session["myRpt_zz"];
       


    }
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.ViewState["SecondBinding"] = false;
        }
        if (myReport != null)
        {
            CrystalReportViewer1.ReportSource = (ReportDocument)Session["myRpt_zz"];
           
        }
    }

其中,增加了红色标记的的语句,点击按钮,提示"未将对象引用设置到对象的实例",如果去掉该语句,是可以查询出报表,但是我关闭该页面后,下一次再打开该页面,上一次查询的结果还留在页面上,我希望,下一次查询时,上一次的结果不要出现,代码应当如何改啊. --------------------编程问答-------------------- 各位,帮忙看一下啊 --------------------编程问答-------------------- 不要出现?。。

if (myReport != null)
  {
  CrystalReportViewer1.ReportSource = (ReportDocument)Session["myRpt_zz"];
    
  }
这边去掉。。 --------------------编程问答-------------------- 把下面的代码去掉,翻页会有问题啊
if (myReport != null)
  {
  CrystalReportViewer1.ReportSource = (ReportDocument)Session["myRpt_zz"];
    
  } --------------------编程问答-------------------- 你每次查询之前把数据集清空就好啦,不需要释放资源。。。
比如我的:
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int MemberId = 0;
                if (textBox1.Text != "")
                {
                    Mydata.Clear();
                    MemberId = Convert.ToInt32(textBox1.Text.Trim());
                    if (member.Exists(MemberId))
                    {
                        da = new MySqlDataAdapter("select a.MemberId as MemberId,MemberName,type, if(type=0, CAST(Score as CHAR(20)) ,if(Score=0,'不合格','合格') ) as Score  from member as a left join memscore as b on a.memberid = b.memberid where a.memberid=" + MemberId + "", conn);
                        da.Fill(Mydata, "MyReportDataTable");

                        da1 = new MySqlDataAdapter("select * from assessmentinfo where MemberId=" + MemberId + "", conn);
                        da1.Fill(Mydata2, "DataScoreInfo");
                        crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");
                        //crystalReport11.Refresh();
                        crystalReport11.SetDataSource(Mydata);
                        crystalReportViewer1.ReportSource = crystalReport11;
                        crystalReportViewer1.ShowPrintButton = false;
                        
                    }
                    else
                    {
                        MessageBoxEx.Show("您输入的Id号错误!");
                        return;
                    }
                }
                else
                {
                    MessageBoxEx.Show("请输入您的Id号!");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBoxEx.Show(ex.Message);
            }
           
        } --------------------编程问答-------------------- 哦 其实我的做法好像不能解决lz的问题,我自己就是在load事件里面设置有默认值查询的报表显示。。。只是那个默认的数值查询出来是空数据而已。。。 --------------------编程问答-------------------- 刚才试了下,好像不行, --------------------编程问答-------------------- 你把你的load事件中代码,给我看一下呢,我也可以参考一下,默认是一个空表格. --------------------编程问答-------------------- load中的代码是一样,只是那个MemberId在load事件中我默认设置为1,这在数据库当中是没有查询不到结果的,所以看起来就是空的而已。。。 --------------------编程问答-------------------- 还有,如果load事件中已经出现了crystalReport11.Load(Application.StartupPath + "CrystalReport1.rpt");
这句代码,那么点击查询事件里面就别加这句了,因为我遇到过这个问题。。 --------------------编程问答-------------------- 所以,你的Mydata.Clear();加不加,其实效果是一样的. --------------------编程问答-------------------- 加了有一个好处,就是如果第一次点击查询查询有数据了,如果我改掉文本框的memberId再点击查询就必须先清空的。。 --------------------编程问答-------------------- 有没有其它方法啊,这样还是长时间连着数据库的,如果多人,同时查询,数据查询会不会慢啊,
阿泰,有什么其它好方法啊. --------------------编程问答-------------------- 1: 使用Push方式,报表使用的是断开式记录集,报表跟数据库本身不发生任何联系
SqlDataAdapter ds = new SqlDataAdapter(s);
ds.Fill(dt, "zongzhuang");
这句之后,就可以关闭数据库连接了

所以不存在多人使用,报表对数据库连接的影响

2:当然,因为使用了Session等保持了一个相对较大的对象,这个如果并发多,Session多的话,
确实会影响到性能。
所以也可以考虑用ViewState等缓存在客户端。或使用一些方法,来主动释放Session --------------------编程问答-------------------- 那我如果希望这个页面,查询过后,关闭该页面后,第二次再打开该页面.希望数据清空,不要保留上次的数据,是不是只能用楼上的方法,在LOAD中,查一个不存在的内容啊. --------------------编程问答--------------------

 protected void Page_UnLoad(object sender, EventArgs e)
    {
        myRpt.Dispose();
    }
--------------------编程问答-------------------- protected void Page_UnLoad(object sender, EventArgs e)
    {
        myRpt.Dispose();
    }

加了这个,第一次打开页面,就出错了,“未将对象引用设置到对象的实例”
补充:.NET技术 ,  图表区
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,