我有一個很奇怪的問題:C#「System.IndexOutOfRangeException:有沒有一行位置0」時,使用存儲過程
我使用一個存儲過程數據取出來的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。在C中的AssetMatrix.Default.BindData())中的AssetMatrix.Default.BindData() :\ Users \ Andrei \ Desktop \ Asset Matrix 8 \ Asset_Matrix_8 \ AssetMatrix \ Default.aspx.cs:115 AssetMatrix.Default.Page_Load(Object sender,EventArgs e)位於C:\ Users \ Andrei \ Desktop \ Asset Matrix 8 \ Asset_Matr ix_8 \ AssetMatrix \ Default.aspx.cs:32 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(對象發件人,EventArgs的)51 System.Web.UI.Control.OnLoad(EventArgs的)95 的System.Web .UI.Control.LoadRecursive()+59 System.Web.UI.Page.ProcessRequestMain(布爾includeStagesBeforeAsyncPoint,布爾includeStagesAfterAsyncPoint)2952
** lthough *與調試我得到的值tb_RECMP_Phase.Rows[0]
。
'tb_RECMP_Phase'可能沒問題,但您也使用'tb_MP_Phase'數據表。你在調試器中檢查過它嗎? – Andrei