数据源对象无效

时间:2014-12-28 10:03:20

标签: c# crystal-reports

我收到了这个错误!当打开水晶报告follong代码。

数据源对象无效。

代码在这里

Cursor = Cursors.WaitCursor;
            timer1.Enabled = true;
            CrystalReport2 rpt = new CrystalReport2();
            obj.connection();
            String accept = "SELECT S.Product as Products, COALESCE(Pur_Orders.Quantity, 0) as [Products purchased], COALESCE(Sale_Orders.Quantity, 0) as [Products Sold] From Inv_stock S LEFT JOIN (SELECT Item, SUM(Quantity) AS Quantity FROM Pur_Orders Where Pur_Orders.Date='" + dateTimePicker1.Value.Date + "' GROUP BY Item) AS Pur_Orders ON S.Product = Pur_Orders.Item LEFT JOIN (SELECT Item, SUM(Quantity) AS Quantity FROM Sale_Orders Where Sale_Orders.Date='" + dateTimePicker1.Value.Date + "' GROUP BY Item) AS Sale_Orders ON S.Product = Sale_Orders.Item";
            SqlCommand cmd = new SqlCommand(accept, obj.con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "Inv_stock");
            da.Fill(ds, "Pur_Orders");
            da.Fill(ds, "Sale_Orders");
            rpt.SetDataSource(da);
            obj.con.Close();
            Inventory inv = new Inventory();
            inv.CrystalReportViewer.ReportSource = rpt;
            inv.Visible = true;

什么是问题?????

2 个答案:

答案 0 :(得分:1)

您正在使用DataAdapter设置报告的数据源:

  
    

rpt.SetDataSource(DA);

  

将其替换为数据集或数据表,如下所示:

  
    

rpt.SetDataSource(DS); //数据集

         

rpt.SetDataSource(ds.Tables [ “表名”]); //数据表

  

答案 1 :(得分:-1)

您应该创建嵌套查询的视图并检查它,然后在您将新报表作为数据时调用该特定视图。 如果您的查询是正确的但数据课程无效,您将无法在报告中获得所需的输出..