【问题标题】:Web API throwing unhandled exception errorWeb API 抛出未处理的异常错误
【发布时间】:2016-10-12 18:27:39
【问题描述】:

我通过 Web API 进行了以下数据库调用:

public IEnumerable<PogStoreData> GetStoreSetData(string FloorplanIdList, string PogIdList, string PogName, string Season, string Version,
            string PogStatusList, Nullable<DateTime> IssueDate, Nullable<DateTime> StartDate, Nullable<DateTime> LiveDate, 
            Nullable<DateTime> ExpirationDate, string PogMoveTypeList)
{
    // Create the parameters collection
    var parameters = new Collection<SqlParameter>();

    // Add each parameter.  Entity will not work without all params in the correct order
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@fpIdList", string.IsNullOrEmpty(FloorplanIdList) ? null : FloorplanIdList, -1));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@pogIdList", string.IsNullOrEmpty(PogIdList) ? null : PogIdList, -1));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@pogName", string.IsNullOrEmpty(PogName) ? null : PogName, 100));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@season", string.IsNullOrEmpty(Season) ? null : Season, -1));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@version", string.IsNullOrEmpty(Version) ? null : Version, -1));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@pogStatusList", string.IsNullOrEmpty(PogStatusList) ? null : PogStatusList, 10));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@issueDate", IssueDate.HasValue ? IssueDate.Value.ToShortDateString() : null, 20));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@startDate", StartDate.HasValue ? StartDate.Value.ToShortDateString() : null, 20));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@liveDate", LiveDate.HasValue ? LiveDate.Value.ToShortDateString() : null, 20));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@expirationDate", ExpirationDate.HasValue ? ExpirationDate.Value.ToShortDateString() : null, 20));
    parameters.Add(StoredProcedureParameterBuilder.StringParam("@pogMoveList", string.IsNullOrEmpty(PogMoveTypeList) ? null : PogMoveTypeList, 10));

    return SelectList<PogStoreData>("spc.SPA_get_store_set_data_by_criteria", parameters);
}

下面是SelectList的代码:

private IEnumerable<T> SelectList<T>(string inStoredProcedure, ICollection<SqlParameter> inParameters)
{
    string paramNames = string.Join(",", inParameters.Select(parameter => parameter.ParameterName).ToList());
    string sqlString = inStoredProcedure + " " + paramNames;
    object[] paramValues = inParameters.Cast<object>().ToArray();

    if (paramValues.Length > 0)
        return this.Database.SqlQuery<T>(sqlString, paramValues).ToList();
    else
        return this.Database.SqlQuery<T>(sqlString).ToList();
}

但是当我只为 PogStatus 列表传递值时,我收到以下错误: 转换为值类型“System.Double”失败,因为具体化值为 null。 我在 SqlQuery(sqlString).ToList() 语句中收到上述错误。 我不知道如何解决这个问题。请帮忙。

【问题讨论】:

    标签: c# asp.net-web-api


    【解决方案1】:

    最可能的原因是您的PogStoreData 类型中有一个double 类型的字段/属性,但查询返回null。

    尝试直接在数据库中(例如在 Management Studio 中)执行此存储过程,并将结果与​​您希望收到的数据进行比较。

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2011-10-10
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多