2011-09-30 48 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

相关问题