我有一个奇怪的错误让我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