如何在同一表单中的不同事件中共用一个dataset?
、在表单入口处定义了
public DataSet myds2 = new DataSet ();
2、在“查询”控件里生成了myds2(这是第一个按钮的事件)
DataSet myds2 = mybase.getds("select * from sb where sbbh='" + this.textBox7.Text.Trim() + "'and nf='" + this.numericUpDown4.Value.ToString().Trim() + "'and yf='" + this.numericUpDown3.Value.ToString().Trim() + "'", "table1");
if (myds2.Tables[0].Rows.Count != 0)
{}
3、想在“打印”控件中直接将上一步生成的myds2做为水晶报表的数据源来进行打印(这是第二个按钮的事件)
private void button4_Click_1(object sender, EventArgs e)
{
ReportDocument rptDoc = new ReportDocument();
string rptPath = "";
string serverPath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\")).LastIndexOf("\"));
rptPath = serverPath + @"\zlsfp.rpt";
rptDoc.Load(rptPath);
rptDoc.SetDataSource(myds2.Tables [0]); //在这儿直接应用刚才第二步生成的myds2
rptDoc.Refresh();
this.crystalReportViewer1.ReportSource = rptDoc;
}
运行时报错:无法找到表 0。
请问怎么解决。谢谢
[解决办法]
还没解决吗?作为资深.net开发,很久没来帮同学们解答问题了。
我觉得我应该为这个社区做点贡献了,就从你开始吧
两个事件为什么不能共用一个数据的原因是,每次请求都会生成一个新的对象实例(新的会话),只有静态数据或Session,Cookie之类才能被重新使用(实际上都是直接或间接的通过客户端cookie的方式来实现的)
[解决办法]
呀,一不小心直接发出去了
现在来说怎么解决你的问题
很简单,把你获取这个DataSet的这个函数提取出来另作一个函数
两个事件需要用这个数据时直接调用即可
当然你也可以再第一次获取这个DataSet后把数据存储到静态DataSet中
在后面的请求中就能用了,明白?
[解决办法]
将myds2 定义成全局变量……