通过参数提取结果时出错

时间:2013-11-18 19:16:04

标签: sql sql-server

我试图通过下拉选项提取结果,该选项包含今天,每周,每月,每年的日期范围选项

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

1 个答案:

答案 0 :(得分:0)

由于EndDate可以为空,因此在声明时必须为其赋值(即使为null)。

DateTime? EndDate = null;

至于第二个错误,你应该使用可空的属性Value(你应该先检查它是否有值)来获取DateTime对象:

if (EndDate.HasValue)
    DataTable dtbl = BIZ.ReportsBIZAdmin.ReportsCityFilter(StartDate, EndDate.Value).Tables[0];