0

我打算從視圖模型調用存儲過程到ActionResult中。索引超出範圍異常(在位置0處沒有行)

當我試圖運行我的代碼我得到一個錯誤

IndexOutOfRangeException是由用戶代碼來處理 - 在位置0

沒有行這是一個新創建的表是空的但爲什麼存儲過程不會被觸發來插入數據?這是我的代碼。

調用過程CreateSecureRequestOutcome

public static void CreateSecureRequestOutcome(
OracleTransaction trans, 
dsAdmin.SECURE_REQUEST_OUTCOMESRow outcomeRow) 
    { 
    using (OracleCommand cm = new OracleCommand()) 
    { 
    cm.Connection = trans.Connection; 
    cm.Transaction = trans; 
    cm.CommandText = "TheService.PKG#SECURE_REQUEST.SECURE_REQUEST_OUTCOME"; 
    cm.CommandType = CommandType.StoredProcedure; 
    cm.AddToStatementCache = true; 

OracleParameter param = cm.Paramaters.Add("P_KEEP_PERSON_ID", OracleDBType.Decimal, Paremeter.Direction.Input); 
param.Value = keepPersonID; 

param = cm.Paramaters.Add("P_SECURE_REQUEST_GUID", OracleDBType.Raw, 16, null, Paremeter.Direction.Input); 
param.Value = outcomeRow.SECURE_REQUEST_GUID; 

param = cm.Parameters.Add("P_OUTCOME_TIMESTAMP", OracleDBType.Object, Paremeter.Direction.Output); 
    } 
} 
cm.ExecuteNonQuery(); 

IdentityConfirmed視圖模型控制器

[httpPost] 
public ActionResult IdentityConfirmed(FormCollection collection) 
{ 
dsAdmin.SECURE_REQUESTS_OUTCOMESRow secReqOutRow; 

using (OracleConnection cn = new OracleConnection (OracleConnectionManager.GetProxyServiceConnetionString())) 
{ 
    cn.Open(); 
    using (OracleTransaction trans = cn.BeginTransaction()) 
    { 
      using (Data.dsAdminTableAdapters.SECURE_REQUESTS_OUTCOMESTableAdapter taOut = new Data.dsAdminTableAdapters.SECURE_REQUESTS_OUTCOMESTableAdapter(); 
      { 
      typedDatasetFiller.ApplyConnection(taOut, cn); 
      secReqOutRow = taOut.GetDataBySecureGUID(request.ToByteArray())[0]; 
      } 
      secReqOutRow.OUTCOME_TIMESTAMP = DateTime.Now.AddMonths(1); 
      Support.CreateSecureRequestOutcome(trans, secReqOutRow); 
      trans.Commit(); 
     } 
    cn.Close(); 
    } 
retrun View("IdentityConfirmed", vm); 
} 
+0

你有沒有在Google搜索你的例外訊息?在哪一行你會得到這個錯誤? –

+0

我在這裏得到錯誤secReqOutRow = taOut.GetDataBySecureGUID(request.ToByteArray())[0]; – Gee

+0

在將索引器添加到索引器之前,您應該首先檢查'secReqOutRow'是否包含數據。 – Abbas

回答

1

嗯,你覺得錯誤意味着什麼?你爲什麼不正確調試?

爲了簡短起見,GetDataBySecureGUID返回一個空數組,因此訪問索引0無效。


請改變你的代碼如下:

var results = taOut.GetDataBySecureGUID(request.ToByteArray());   
secReqOutRow = results[0]; 

現在設置一個斷點到第二行和檢查的results值。那裏有幾個元素?

我敢打賭,沒有,因爲這正是例外的意思。您正嘗試使用大於最大允許索引的索引訪問集合中的元素。事實上,你試圖訪問第一個元素並得到這個錯誤意味着沒有第一個元素,所以在集合中根本沒有任何元素。

+0

其實我在這一行放了一箇中斷,它返回一個字節數值。 xx00xxxxx-x0xx-xxxx-0120-xxxx-xxxxx2xx0xxx,數字/字母。所以有值 – Gee

+0

'request.ToByteArray()'可能包含一個值,但是'GetDataBySecureGUID'返回一個空的數組/列表。我建議你在一秒鐘內調試我要寫入我的答案的內容,然後告訴我有數據。 –

+0

你的權利是0,我對C#如此陌生,我如何獲取數據? – Gee