2015-01-07 33 views
0

我想从数据库中获取日期值并将其作为报表参数发送到报表中。但如果我现在这样做,我得到一个错误(为报告参数提供的值不适用于其类型日期时间)。我目前使用组合框来包含日期列表,并且我希望将选定的项目作为日期参数发送到报告中。我正在使用c#,并且正在使用rdlc报告文件。这是我的代码。将组合框项目作为报表日期参数发送到报表中

this.rptStockMismatch.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerURL"]); 
    this.rptStockMismatch.ShowParameterPrompts = false; 

    ReportParameter[] reportParameter = new ReportParameter[2]; 
    reportParameter[0] = new ReportParameter("StockCaptureDate", this.cboStockCaptureDate.SelectedItem.ToString()); //this is where I want to insert the selected combobox itme in. 
    reportParameter[1] = new ReportParameter("DepotID", this.cboDepot.SelectedValue.ToString()); 

this.rptStockMismatch.ServerReport.SetParameters(reportParameter); 
this.rptStockMismatch.RefreshReport(); 
+0

尝试组合框的值转换为'datetime' –

+0

@POHH我试过,但我仍然得到同样的错误 – Scelo

回答

0

我在这里同意POHH。错误消息说你需要一个DateTime对象。

虽然您将SelectedItem转换为字符串。尝试搜索.ToDateTime()函数,如果没有,尝试编写一个分析字符串的小函数。

//Assuming a Date in this fashion: DD.MM.YYYY 
string[] splitDate = cboStockCaptureDate.SelectedItem.ToString() 
    .Split(".".ToCharArray()); 

DateTime finalDate = new DateTime(Int32.Parse(splitDate[2]), Int32.Parse(splitDate[1]), Int32.Parse(splitDate[0])); 

然后将其添加到您的ReportParameters中。

请注意,如果字符串不是纯数字,Int32.Parse可能会抛出一些异常。

+0

错误:索引阵列 – Scelo

+0

的边界之外@Scelo这可能是由您的日期,格式看起来比不同引起的我用过的那个。你的日期怎么样?也许尝试调试并查看字符串数组并相应地编辑构造函数。 – SchwarzSkills

+0

它应该是cboStockCaptureDate.SelectedItem.Value.ToString(),并且无论如何,在调试模式下停止代码并查看“Value”的值以查看如何最好地投射它。 – InitK

相关问题