我有一個奇怪的錯誤讓我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)犯罪嫌疑人線,但事實並非行號表示堆棧所在的行號。
我並不總是測試我的代碼;但是當我這樣做時,我會在生產中做到這一點。 –
注意:不要使用Type.GetType(「System.Int64」),而應使用'typeof(long)'或'typeof(System.Int64)'。 –
'輸入字符串格式不正確'告訴我有什麼東西需要一個字符串,但它應該實際上期待和'對象'或'System.Int64'大聲笑@測試在產品 – Zasz