2011-09-30 44 views
0

我有一個奇怪的錯誤讓我buffeled: 我有一個函數..uhm ......是這樣的:長時間轉換爲System.Int64會拋出錯誤?

void someFunctionTakingALong(long ID) 
{ 
    var table = new DataTable(); 
    table .Columns.Add(new DataColumn("RID", Type.GetType("System.Int64"))); 
    table.Rows.Add(ID);  //<-- throws err 
} 

拋出這個錯誤:

System.ArgumentException: Input string was not in a correct format.Couldn't store in RID Column. Expected type is Int64. ---> System.FormatException: Input string was not in a correct format. 
    at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) 
    at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt) 
    at System.String.System.IConvertible.ToInt64(IFormatProvider provider) 
    at System.Data.Common.Int64Storage.Set(Int32 record, Object value) 
    at System.Data.DataColumn.set_Item(Int32 record, Object value) 
    --- End of inner exception stack trace --- 

這發生在生產,並且我沒有日誌來查看實際傳遞給該函數的ID的值是什麼......但即使如此..如果參數很長,應至少保證ID很長......對吧?那爲什麼這個投擲? ID可以具有哪些值不能轉換爲int64?

編輯:

下面是實際的來源:Trhowing列PropValueID

public void AddRule(int PropID, long PropValueID, int type, string Data) 
     { 
      if (!m_HasRule) 
      { 
       m_Rules = new DataTable(); 
       m_Rules.Columns.Add(new DataColumn("RID", Type.GetType("System.Int32"))); 
       m_Rules.Columns.Add(new DataColumn("PropID", Type.GetType("System.Int32"))); 
       m_Rules.Columns.Add(new DataColumn("PropValueID", Type.GetType("System.Int64"))); 
       //m_Rules.Columns.Add(new DataColumn("PropValue", Type.GetType("System.String"))); 
       m_Rules.Columns.Add(new DataColumn("Type", Type.GetType("System.Int32"))); 
       m_Rules.Columns.Add(new DataColumn("Data", Type.GetType("System.String"))); 
       m_HasRule = true; 
      } 
      ToStringSB = null; 

      if (type<2) // a "Is/Not specified property" 
      {    
       if (string.IsNullOrEmpty(Data)) //is specified (0,1) 
        m_Rules.Rows.Add(m_RID, PropID, 0, type, "");  //<<-- here we pass 0 (int32) instead of long.. could this be it? Stack says this is not the line throwing 
       else //is equal to/is not equal to(2,3) 
        m_Rules.Rows.Add(m_RID, PropID, PropValueID,3-type, Data);     
      }else 
       if ((type > 3) && (type < 6)) // a "like/not like" rule 
       { 
        // "Like" or "not like" DATA .. no value ID is provided 
        m_Rules.Rows.Add(m_RID, PropID, PropValueID, type, Data); //<<-- Stack says it throws here 

       } 
       else // a greater/lesser rule 
       { 
        m_Rules.Rows.Add(m_RID, PropID, PropValueID, type, Data); 
       } 

     } 

有一種情況我們通過對文字0(一個int)犯罪嫌疑人線,但事實並非行號表示堆棧所在的行號。

+14

我並不總是測試我的代碼;但是當我這樣做時,我會在生產中做到這一點。 –

+6

注意:不要使用Type.GetType(「System.Int64」),而應使用'typeof(long)'或'typeof(System.Int64)'。 –

+1

'輸入字符串格式不正確'告訴我有什麼東西需要一個字符串,但它應該實際上期待和'對象'或'System.Int64'大聲笑@測試在產品 – Zasz

回答

7

無法重現 - 這工作得很好,我:

using System; 
using System.Data; 

class Test 
{ 
    static void Main() 
    { 
     long x = 10; 
     var table = new DataTable(); 
     table.Columns.Add(new DataColumn("RID", Type.GetType("System.Int64"))); 
     table.Rows.Add(x); 
    } 
} 

請注意,您可以通過使用typeof(long)擺脫調用到Type.GetType()

它看起來像是由於某種原因,它將值轉換爲一個字符串並返回,這是真的奇怪......你確定它只是從那個代碼?

如果你能想出一個簡短但完整的例子像我的但失敗了,這真的會有所幫助。

1

您確定您將long傳遞給DataRowCollection.Add方法嗎?

你發佈的代碼片段沒有什麼幫助,你能發佈實際的代碼嗎?

System.Data命名空間代碼預先通用的泛型,所以有很多拳擊和拆箱正在進行,我認爲這是無法解決的。從堆棧跟蹤中,我們可以看到代碼試圖從Object值中設置DataRow列的值,並且這導致String解析爲Int64。這導致我認爲對table.Rows.Add的調用未收到RID列的long值。除非拳擊混淆了long的值,並最終成爲一個空的String,儘管這不是不可能的,但這是非常不可能的。我的意思是,我沒有編寫.Net Framework代碼,我不知道它在內部做了什麼,總的來說,我們相信微軟做得很好,但是在所有的代碼中都存在錯誤的可能性。所以無論是在你的代碼還是在微軟的代碼中。這個可能性不利於你。

+0

嗯......長期轉換爲無效/空字符串的情況會是怎樣的情況?有沒有特殊的情況?即。 #INF #NAN? – Radu094

+0

你確定這是生產中的代碼嗎?我沒有看到將行添加到集合的「喜歡/不喜歡規則」和「更大/更小規則」代碼中的差異。從評論中說'沒有提供價值ID'。難道實際的代碼是:'m_Rules.Rows.Add(m_RID,PropID,「」,type,Data);'或者類似的東西? – Xint0

相關問題