开发者

Convert long to System.Int64 throws error? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
开发者_StackOverflow

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 5 years ago.

Improve this question

I got a strange error that got me buffeled: I have a function ..uhm... something like this:

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

throws this error:

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 ---

This happens in production, and I don't have logs to see what value of ID is actually passed to the function... but even so.. having the parameter a long should AT LEAST guarantee that ID is a long ... right? So why is this throwing? What values could ID have that are not convertible to a int64 ?

EDIT:

Here is the actual source : Trhowing column is 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);
                }

        } 

There is a suspect line where we pass on literal 0 (an int) but that's not the line number where the stack says it throws.


Unable to reproduce - this works fine for me:

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);
    }
}

Note that you can get rid of the call to Type.GetType() by using typeof(long).

It looks like for some reason it's converting the value to a string and back, which is really strange... are you sure it's from just that code?

If you can come up with a short but complete example like mine but which fails, that would really help.


Are you sure you are passing a long to the DataRowCollection.Add method?

The code snippet you posted doesn't help much, can you post the actual code?

The System.Data namespace code pre-dates generics, so there's a lot of boxing and unboxing going on, and I think there's no way around it. From the stack trace we can see that the code is trying to set the value of a DataRow column from an Object value, and that this results in a String parse to Int64. This leads me to believe that the call to table.Rows.Add is not receiving a long value for the RID column. Unless the boxing messed up the long value and ended up as an empty String, which although is not impossible is extremely improbable. By that I mean, I didn't code the .Net Framework, I don't know what does it do internally, in general we trust that Microsoft did a good job, but in all code there's the possibility of error. So either the bug is in your code or in Microsoft's code. The odds are against you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜