我正试图通过下拉选择来提取结果,该下拉选择具有日期范围选项,如今天、每周、每月、每年
protected void ddlFilter_SelectedIndexChanged(object sender, EventArgs e)
{
DateTime StartDate;
DateTime? EndDate;
if (ddlFilter.SelectedValue == "today")
{
//StartDate = Convert.ToInt32(ddlFilter.SelectedValue)
StartDate = DateTime.Today;
EndDate = DateTime.Today;
}
if (ddlFilter.SelectedValue == "weekly")
{
StartDate = DateTime.Today;
EndDate = DateTime.Now.AddDays(-7).Date;
}
if (ddlFilter.SelectedValue == "byweekly")
{
StartDate = DateTime.Today;
EndDate = DateTime.Now.AddDays(-15).Date;
}
if (ddlFilter.SelectedValue == "monthly")
{
StartDate = DateTime.Today;
EndDate = DateTime.Now.AddMonths(-1).Date;
}
if (ddlFilter.SelectedValue == "yearly")
{
StartDate = DateTime.Today;
EndDate = DateTime.Now.AddYears(-1).Date;
}
FillGridFilter(StartDate, EndDate);
}
private void FillGridFilter(DateTime StartDate, DateTime? EndDate)
{
if (EndDate != null)
{
hdnid.Value = EndDate.ToString();
}
grdCrew.DataSource = null;
DataTable dtbl = BIZ.ReportsBIZAdmin.ReportsCityFilter(StartDate, EndDate).Tables[0];
if (dtbl != null && dtbl.Rows.Count > 0)
{
grdCrew.DataSource = dtbl;
grdCrew.DataBind();
ViewState["griddata"] = dtbl;
}
else
{
grdCrew.DataSource = null;
grdCrew.DataBind();
ViewState["griddata"] = null;
}
}
现在它在"FillGridFilter(StartDate, EndDate);"
上给出的错误是Use of unassigned local variable 'EndDate
'和Use of unassigned local variable 'EndDate'
和
DataTable dtbl = BIZ.ReportsBIZAdmin.ReportsCityFilter(StartDate, EndDate).Tables[0];
说The best overloaded method match for 'BIZ.ReportsBIZAdmin.ReportsCityFilter(System.DateTime, System.DateTime)' has some invalid arguments
存储过程似乎运行良好,它是:
if @end_date = 'today'
begin
select jp.id, city.name[City]
from rs_job_posting jp
inner join rs_job_posting_location jpl on jpl.id = jp.id
inner join rs_cor_city city on city.id = jpl.city_fk
inner join rs_organization_detail od on od.id = jp.id
where posting_date between '@start_date' and '@end_date'
order by no_of_posts Desc
END
因为EndDate
是可以为null的,所以在声明它时必须为它赋值(甚至为null)。
DateTime? EndDate = null;
至于第二个错误,您应该使用nullable的属性Value
(您应该首先检查它是否有值)来获得DateTime
对象:
if (EndDate.HasValue)
DataTable dtbl = BIZ.ReportsBIZAdmin.ReportsCityFilter(StartDate, EndDate.Value).Tables[0];