SQL Reporting: Null Parameter

99封情书 提交于 2019-12-12 11:04:51

问题


I have discovered that in SQL Reporting there might be a problem. I have a ReportViewer on my page and I am sending in parameters using the following method:

List<ReportParameter> myParams = new List<ReportParameter>();

myParams.Add(new ReportParameter("Start_Date", StartDate));
myParams.Add(new ReportParameter("End_Date", EndDate));

ReportViewer1.ServerReport.SetParameters(myParams);

This works great! But, when I try to set a parameter to null, after running that query, it maintains the previous value rather than setting it to null.

I run this code on another event that executes after the above code:

List<ReportParameter> myParams = new List<ReportParameter>();

myParams.Add(new ReportParameter("Start_Date")); 
// I even tried omiting this line.  
//(This is the null parameter I wish to pass)
myParams.Add(new ReportParameter("End_Date", EndDate));

ReportViewer1.ServerReport.SetParameters(myParams);

Has anyone come across a work around or a different technique to get this working?

Also if I initially do not define the parameter, then assign the parameter, then do not define the paramter, it maintains the value that was assigned. (These are all postbacks, each event)


回答1:


Do something like this..I've tested it in my own little test project and it seems to work.

List<ReportParameter> myParams = new List<ReportParameter>();

ReportParameter p = new ReportParameter("Start_Date");
p.Values.Add(null);
myParams.Add(p);

//myParams.Add(new ReportParameter("Start_Date")); 
// I even tried omiting this line.  
//(This is the null parameter I wish to pass)
myParams.Add(new ReportParameter("End_Date", EndDate));

ReportViewer1.ServerReport.SetParameters(myParams);



回答2:


Have you tried calling:

ReportViewer1.Reset();

in between the two calls?




回答3:


Are those StartDate & EndDate variables of type DateTime? Maybe this has to do with the fact that DateTime variables cannot be set to null, they are DateTime.MinValue instead. Try setting the parameter to DateTime.MinValue and handle accordingly.



来源:https://stackoverflow.com/questions/715090/sql-reporting-null-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!