2013-01-23 58 views
1

的ASP.NET報表我有一個名爲「Graph」的存儲過程,它應該獲得參數@Material的值,並且我在ASP.NET中創建了一個報表,該報表應該使用存儲過程中的數據顯示圖表。帶有參數

但是,當我嘗試加載的報告,我得到:報表處理期間發生

錯誤。 無法創建到數據源'PhilipsMaterialsDataSet'的連接。 ObjectDataSource'ObjectDataSource1'無法找到沒有參數的非泛型方法'GetData'。

我嘗試了不同的解決方案,但都沒有工作。另外,我不確定是否應該在ASP代碼中聲明參數。

(順便說一下,的GetData這裏不認可,因爲它有一個參數(@材質 - 從存儲過程),由於某種原因,它被稱爲不帶任何參數)

背後的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Microsoft.Reporting.WebForms; 


public partial class StatisticsPage : System.Web.UI.Page 
{ 
    string Connectionstring = "server=(local)\\SQLEXPRESS;database=PhilipsMaterials;Integrated Security=SSPI"; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 

     } 

    } 

    protected void btndisplay_Click(object sender, EventArgs e) 
    { 
     BindReport(); 
    } 
    private void BindReport() 
    { 
     SSRSReport report = new SSRSReport(); 
     SqlParameter[] sqlParams = new SqlParameter[] { 
     new SqlParameter("@Material","453567068441") 

     }; 
     string ReportDataSource = "DataSet1"; 
     bool bind = report.CreateReport(Connectionstring, "graph", sqlParams, ref ReportViewer1, ReportDataSource); 
     if (bind) 
     { 
      ReportViewer1.Visible = true; 
     } 
    } 
} 


public class SSRSReport 
{ 


     public SSRSReport() 
     { 
       // 
       // TODO: Add constructor logic here 
       // 
     } 
    public bool CreateReport(String Connectionstring,string StoreProcedureName ,SqlParameter[] Parameter,ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer,string ReportDataSource) 
    { 
     bool reportbind = false; 
     using (SqlConnection con = new SqlConnection(Connectionstring)) 
     { 

      SqlCommand com = new SqlCommand(); 
      com.Connection = con; 
      com.CommandType = CommandType.StoredProcedure; 
      com.CommandText = StoreProcedureName; 
      com.Parameters.AddRange(Parameter); 
      DataSet ds = new DataSet(); 
      SqlDataAdapter da = new SqlDataAdapter(com); 
      da.Fill(ds); 
      ReportDataSource datasource = new ReportDataSource(ReportDataSource, ds.Tables[0]); 
      if (ds.Tables[0].Rows.Count > 0) 
      { 
       ReportViewer.LocalReport.DataSources.Clear(); 
       ReportViewer.LocalReport.DataSources.Add(datasource); 





       //This is another solution I tried: 





       //List<ReportParameter> lstReportParameters = new List<ReportParameter>(); 
       //ReportParameter objReportParameter = new ReportParameter("Material", "453567068441"); 
       //lstReportParameters.Add(objReportParameter); 
       //ReportViewer.LocalReport.SetParameters(lstReportParameters); 
       // ReportViewer.ServerReport.Refresh(); 

       reportbind = true; 
      }      
     } 
     return reportbind; 
    } 

} 

ASP代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StatisticsPage.aspx.cs" Inherits="StatisticsPage" %> 

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title>Test SSRS</title> 
</head> 
<body> 
     <form id="form1" runat="server"> 
    <div> 

    </div> 
     <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="610px" Width="1179px" ShowParameterPrompts="true"> 
      <LocalReport ReportPath="Report.rdlc" > 
       <DataSources> 
        <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" /> 
       </DataSources> 
      </LocalReport> 
     </rsweb:ReportViewer> 
     <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" > 
     </asp:ObjectDataSource> 
      <asp:ScriptManager ID="ScriptManager1" runat="server"> 
      </asp:ScriptManager> 
    </form> 
</body> 
</html> 
+0

至於提到的錯誤,你的'ObjectDataSource'正在尋找方法'GetData',這沒有按」不存在。你應該看看ObjectDataSource是如何工作的。 – rikitikitik

+0

爲什麼使用'com.Parameters.AddRange(Parameter);'爲什麼不使用'com.Parameters.AddWithValue()'方法..?在vb的AddRange會工作,但我有這個問題之前做從VB.NET到C#的轉換工作,並且你不需要AddRange – MethodMan

+0

謝謝...順便說一句,GetData在這裏不被識別,因爲它有一個參數( @材料 - 來自存儲過程),由於某種原因,它被調用時沒有任何參數。 – Onca

回答

0
public DataSet CreateReport(String Connectionstring,string StoreProcedureName ,SqlParameter[] Parameter,ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer,string ReportDataSource) { 

//return the dataset 

} 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="CreateReport" TypeName="SqlHelper" /> 

ü可以嘗試烏爾方法的返回值更改爲數據集.. 並在代碼隱藏試着數據綁定到該方法..

也許它可能工作..

+0

我只是改變綁定從getdata創建報告真的.. cud不是很多的幫助,但仍值得一試,因爲錯誤指向getdata,所以只是猜測,它的..也許不是一個真正的解決方案,不能告訴在這裏,這確實有點複雜,需要更多時間來解決,試圖讓最基本的東西擺脫困境,看看所有複雜的東西是否落實到位。 – user1974729