1、在表单入口处定义了
 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。
请问怎么解决。谢谢

解决方案 »

  1.   

    rptDoc.SetDataSource(myDs2.Tables["table1"]);改成这样试试。
      

  2.   

    1、将DataSet设为共用区域的全局变量;
    2、在调用该变量前绑定数据源(Web里注意IsPostback属性的使用);
      

  3.   

    public DataSet myds2 = new DataSet(); 
    我已经设为共用的全局变量了,而且是在同一表单中调用,
    我做的是winform的
      

  4.   

    报错是在那一句上??????因为你这个是中间截取的,我也不好写代码。
    而且我一般是用SqlDataAdapter做一个Fill(DataSet,"表名");如果报错为“未将对象引用设置到对象的实例”,一般来说是不是你运行到这一句的时候,是不是已经将对象实例释放掉了?或者指向不正确?
      

  5.   

    楼上说的很对,我已经解决了,本来生成dataset这调用的另一个类中的方法,在这个方法中的dataset又被实例化了一次,可能是这个原因,导制不能在其他事件中调用了。我改为将Fill(DataSet,"表名"); 中的dataset作为一个参数传过来,仍然用以前实例化的dataset,一下子就解决了这个问题。也让我更深刻的理解了这种方式。
    谢谢大家