我的报告查看器出错,我不知道如何更正.... 我得到的错误是:“没有为数据源'Request'提供数据源实例。” 我试过寻找这个,但找不到任何我能看到的错误,有人可以指点我正确的方向吗?这就是我的reportvierer的样子:
<rsweb:ReportViewer ID="rptViewer" runat="server" Height="654px"
Width="924px" Font-Names="Verdana" Font-Size="8pt"
InteractiveDeviceInfos="(Collection)" WaitMessageFont-Names="Verdana"
WaitMessageFont-Size="14pt">
<LocalReport ReportPath="Report.rdlc">
</LocalReport>
这是我用于reportviewer的代码:
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
if (!Page.IsPostBack)
{
runRptViewer();
}
}
private DataTable getData()
{
DataSet dss = new DataSet();
string sql = "";
sql = "SELECT CC_Request.Company, CC_Request.Attention, CC_Request.Telephone, CC_Request.Email, CC_Items.Model, CC_Items.SerialNo, CC_ItemsRequested.Item, CC_ItemsRequested.Quantity, CC_ItemsRequested.Price, CC_ItemsRequested.ModelID, CC_Items.RequestID FROM CC_Request INNER JOIN CC_Items ON CC_Request.RequestID = CC_Items.RequestID INNER JOIN CC_ItemsRequested ON CC_Items.ModelID = CC_ItemsRequested.ModelID WHERE (CC_Request.Company = 'Alpha') AND (CC_ItemsRequested.ModelID = 20) AND (CC_Items.RequestID = 1)";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dss);
DataTable dt = dss.Tables[0];
return dt;
}
private void runRptViewer()
{
this.rptViewer.Reset();
this.rptViewer.LocalReport.ReportPath = Server.MapPath("Reports\\Report.rdlc");
ReportDataSource rds = new ReportDataSource("dsNewDataSet_Table", getData());
this.rptViewer.LocalReport.DataSources.Clear();
this.rptViewer.LocalReport.DataSources.Add(rds);
this.rptViewer.DataBind();
this.rptViewer.LocalReport.Refresh();
}
非常感谢任何帮助。
EDIT -----------------------
这也不起作用:
private DataTable getData()
{
DataTable dtable = new DataTable();
dtable.TableName = "dtNewDataSet_Table";
string sql = "";
sql = "SELECT CC_Request.Company, CC_Request.Attention, CC_Request.Telephone, CC_Request.Email, CC_Items.Model, CC_Items.SerialNo, CC_ItemsRequested.Item, CC_ItemsRequested.Quantity, CC_ItemsRequested.Price, CC_ItemsRequested.ModelID, CC_Items.RequestID FROM CC_Request INNER JOIN CC_Items ON CC_Request.RequestID = CC_Items.RequestID INNER JOIN CC_ItemsRequested ON CC_Items.ModelID = CC_ItemsRequested.ModelID WHERE (CC_Request.Company = 'Alpha') AND (CC_ItemsRequested.ModelID = 20) AND (CC_Items.RequestID = 1)";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(dtable);
DataTable dt = dtable;
return dt;
}
答案 0 :(得分:8)
我也遇到了这个问题,这就是我来这里的原因。
我找出了问题,我在这里发帖是为了帮助那些遇到此类问题的人。
我在我的代码中添加了数据源,如:
ReportViewer1.LocalReport.DataSources.Clear();
ReportDataSource rd = new ReportDataSource("dsData1", DAL.MyDBModel.snData().Tables[0]);
ReportViewer1.LocalReport.DataSources.Add(rd);
并且它抛出同样的错误。
解决方案:
在我的代码中,我将ReportDataSource的参数从“ dsData1 ”更改为“ dsClientPayList ”(与RDLC中相同)。
ReportDataSource rd = new ReportDataSource("dsClientPayList", DAL.MyDBModel.snData().Tables[0]);
它有效
结论: RDLC和ReportViewer中的DataSource名称必须相同。
由于 快乐编码:)
答案 1 :(得分:3)
您必须设置报告中使用的所有数据源。
在这种情况下,如果只有一个数据源,它应该像这样
ReportDataSource rds = new ReportDataSource("Request", getData());
答案 2 :(得分:0)
我宁愿用&#34;使用&#34;正确的对象处理和错误处理的声明
using (SqlConnection conn = new SqlConnection(connStr)) {
using(SqlCommand comm = new SqlCommand()) {
comm.Connection = conn;
comm.CommandText = sql;
...
try {
conn.Open();
comm.ExecuteNonQuery();
}
catch(SqlException e) {
// in case something is wrong
}
}
}