2009-10-19 36 views
7

Tôi đã cố gắng làm việc này trong một thời gian và tất cả mã ví dụ mà tôi thấy không hoàn toàn làm những gì tôi đang làm.Truyền tham số cho các báo cáo tinh thể trong C#

Tôi có một chương trình trả về bản pdf của báo cáo mà tôi chuyển một bảng dữ liệu đến. Điều này làm việc tốt, ngoại trừ tôi muốn vượt qua nó một vài thông số khác (phạm vi ngày của bảng, số liệu thống kê vv) và tôi chỉ không thể làm cho nó hoạt động. Mã của tôi về cơ bản trông như thế này.

ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); 
myDataReport.Load(@"C:\Layouts\Report.rpt"); 
ParameterField myParam = new ParameterField(); 
ParameterDiscreteValue myDiscreteValue = new ParameterDiscreteValue(); 
myParam.ParameterFieldName = "MyParameter"; 
myDiscreteValue.Value = "Hello"; 
myParam.CurrentValues.Add(myDiscreteValue); 
myDataReport.ParameterFields.Add(myParam); 
myDataReport.SetDataSource(myDataTable); 
Stream returnData = myDataReport.ExportToStream(PortableDocFormat); 
myDataReport.Close(); 
return returnData; 

Tôi đã thêm trường tham số trong tài liệu rpt trong tinh thể, tôi có phải thay đổi bất kỳ thứ gì trong tệp xsd trong C# hay tôi thiếu thứ gì đó hoàn toàn khác?

Rất cám ơn, Andy.

Trả lời

22

Tất cả những gì Mã thông số có thể được thay thế bằng ...

// Set datasource first 
myDataReport.SetDataSource(...) 
// Assign Paramters after set datasource 
myDataReport.SetParameterValue("MyParameter", "Hello"); 

Tôi không thể nhớ nếu các vấn đề trật tự khi thiết lập các nguồn dữ liệu và các thông số. Có thể thử đặt nguồn dữ liệu trước. Các xsd/datasource không có liên quan đến các tham số tinh thể.

UPDATE1

SetParameterValue SAU asignation nguồn dữ liệu hoặc bạn sẽ nhận được thông báo lỗi "Thiếu giá trị tham số."

+1

Đúng vậy! Tôi nghĩ rằng tôi đã thử dòng đó trước đây, nhưng tôi đã đặt nguồn dữ liệu ở vị trí không chính xác như bạn đã chỉ ra. Hoạt động tích cực ngay bây giờ, cảm ơn! –

+0

@Andrew. Bạn có thể vui lòng cho tôi biết nơi bạn đã thực hiện cuộc gọi đến dataSource không? – Unlimited071

+2

Đặt SetParameterValue SAU sau khi mã hóa asignation – Apocatastasis

3
ReportDocument cryRpt = new ReportDocument(); 

TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); 
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); 
ConnectionInfo crConnectionInfo = new ConnectionInfo(); 
Tables CrTables; 

string path = "C:/reportpath/report.rpt"; 
cryRpt.Load(path); 

cryRpt.SetParameterValue("MyDate2", str2); 
cryRpt.SetParameterValue("MyDate", str1); 

crConnectionInfo.ServerName = "server"; 
crConnectionInfo.DatabaseName = "DataBase"; 
crConnectionInfo.UserID = "user"; 
crConnectionInfo.Password = "password"; 

CrTables = cryRpt.Database.Tables; 
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) 
{ 
    crtableLogoninfo = CrTable.LogOnInfo; 
    crtableLogoninfo.ConnectionInfo = crConnectionInfo; 
    CrTable.ApplyLogOnInfo(crtableLogoninfo); 
} 

crystalReportViewer1.ReportSource = cryRpt; 
crystalReportViewer1.Refresh(); 
1
 //create object of crystal report. 
     CrystalReport1 objRpt = new CrystalReport1(); 
     objRpt.SetDataSource(ds); 
     ParameterFields pfield = new ParameterFields(); 
     ParameterField ptitle = new ParameterField(); 
     ParameterDiscreteValue pvalue = new ParameterDiscreteValue(); 
     ptitle.ParameterFieldName = "date"; 
     pvalue.Value = txtcolor.Text; 
     ptitle.CurrentValues.Add(pvalue); 
     pfield.Add(ptitle); 
     crystalReportViewer1.ParameterFieldInfo = pfield; 
     crystalReportViewer1.ReportSource = objRpt; 
     crystalReportViewer1.Refresh(); 
+2

Vui lòng giải thích những gì bạn đã sửa hoặc đang đề xuất. Chỉ cần thêm mã không hữu ích lắm. – Priyesh

Các vấn đề liên quan