C#" System.IndexOutOfRangeException:位置0和#34没有行;何时使用存储过程

时间:2016-04-21 14:25:13

标签: c# asp.net sql-server stored-procedures

我有一个非常奇怪的问题:

我使用存储过程从SQL中获取数据:

'CREATE PROCEDURE [dbo].[GET_RECMP_BY_PH_Code]
@PH_Code NVARCHAR(50)
AS

SELECT  Recurrent_PH_MP.Phase_Code AS Phase_Code,
    MP_BGT.FY_Code AS Fiscal_Year,
    MP_BGT.Budget_Type AS Budget_Type,
    MP_BGT.Currency_Symbol AS Currency,  
    ISNULL(dbo.Get_RECEH_Days_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type), 0) AS EH_Days,
    ISNULL(dbo.Get_RECNH_Days_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type), 0) AS NH_Days,
    ISNULL(ROUND(dbo.Get_RECEH_Val_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type),2), 0) AS EH_Previous_Amount,
    ISNULL(ROUND(dbo.Get_RECNH_Val_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type),2), 0) AS NH_Previous_Amount,
    ISNULL(dbo.Get_Exchange_Rate(MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type),0) AS Rate_Used,
    ISNULL(ROUND(dbo.Get_RECEH_Val_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type)/dbo.Get_Exchange_Rate(MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type),2), 0) AS EH_Amount,
    ISNULL(ROUND(dbo.Get_RECNH_Val_BY_PHCode_FYCode(@PH_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type)/dbo.Get_Exchange_Rate(MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type),2), 0) AS NH_Amount
    FROM Recurrent_PH_MP_Budgets AS MP_BGT INNER JOIN Recurrent_PH_MP
    ON MP_BGT.Recurrent_MP_Code=Recurrent_PH_MP.Recurrent_MP_Code
    WHERE Recurrent_PH_MP.Phase_Code = @PH_Code GROUP BY Recurrent_PH_MP.Phase_Code, MP_BGT.FY_Code, MP_BGT.Currency_Symbol, MP_BGT.Budget_Type;

功能是:

public DataTable get_RECMP_BY_PH_Code(string PH_Code)
{
    string res = string.Empty;
    DataTable tb = new DataTable();
    try
    {
        using (var conn1 = DBConnectionUtil.Create())
        {
            using (SqlCommand cmd = new SqlCommand("GET_RECMP_BY_PH_Code", conn1))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PH_Code", PH_Code);
                using (var da = new SqlDataAdapter())
                {
                    da.SelectCommand = cmd;

                    DataSet ds = new DataSet();
                    da.Fill(ds, "RECMP_By_PH");
                    da.Dispose();
                    tb = ds.Tables[0];
                }
            }
        }

    }
    catch (Exception ex)
    {
        ExceptionHandler.HandleException(ex);
    }
    return tb;
}

在代码中使用它时:

DataTable tb_RECMP_Phase = dm.get_RECMP_BY_PH_Code(phase);
int i = 0;
if (tb_RECMP_Phase != null && tb_RECMP_Phase.Rows.Count > 0)
{
    for (i = 0; i < tb_RECMP_Phase.Rows.Count; i++)
    {
        if (tb_RECMP_Phase.Rows[i]["EH_Days"] != null && tb_MP_Phase.Rows[i]["Fiscal_Year"].ToString() == "FY " + DateTime.Now.Year.ToString())
            phaseData.REC_BT_DAYS += Convert.ToDouble(tb_RECMP_Phase.Rows[i]["EH_Days"]);
        if (tb_RECMP_Phase.Rows[i]["EH_Amount"] != null && tb_RECMP_Phase.Rows[i]["Fiscal_Year"].ToString() == "FY " + DateTime.Now.Year.ToString())
            phaseData.REC_BT_AMOUNT += Convert.ToDouble(tb_RECMP_Phase.Rows[i]["EH_Amount"]);
        if (tb_RECMP_Phase.Rows[i]["NH_Days"] != null && tb_RECMP_Phase.Rows[i]["Fiscal_Year"].ToString() == "FY " + DateTime.Now.Year.ToString())
            phaseData.REC_OTHER_DAYS += Convert.ToDouble(tb_RECMP_Phase.Rows[i]["NH_Days"]);
        if (tb_RECMP_Phase.Rows[i]["NH_Amount"] != null && tb_RECMP_Phase.Rows[i]["Fiscal_Year"].ToString() == "FY " + DateTime.Now.Year.ToString())
            phaseData.REC_OTHER_AMOUNT += Convert.ToDouble(tb_RECMP_Phase.Rows[i]["NH_Amount"]);
    }
}

我收到错误:

  

[IndexOutOfRangeException:位置0没有行。]      System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex)+2003383      System.Data.DataRowCollection.get_Item(Int32 index)+21      C:\ Users \ Andrei \ Desktop \ Asset Matrix 8 \ Asset_Matrix_8 \ AssetMatrix \ Default.aspx.cs中的AssetMatrix.Default.generateFinancePhasesTable():317      C:\ Users \ Andrei \ Desktop \ Asset Matrix 8 \ Asset_Matrix_8 \ AssetMatrix \ Default.aspx.cs中的AssetMatrix.Default.BindData():115      C:\ Users \ Andrei \ Desktop \ Asset Matrix 8 \ Asset_Matrix_8 \ AssetMatrix \ Default.aspx.cs中的AssetMatrix.Default.Page_Load(Object sender,EventArgs e):32      System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)+51      System.Web.UI.Control.OnLoad(EventArgs e)+95      System.Web.UI.Control.LoadRecursive()+59      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+2952

**虽然*使用调试器我得到tb_RECMP_Phase.Rows[0]的值。

1 个答案:

答案 0 :(得分:2)

您正在检查tb_RECMP_Phase中是否有行,但我看不到tb_MP_Phase的相同检查。由于你几乎已经消除了一行中零行的可能性,它可能是另一行。