C# 正在尝试将BigInt64项添加到listbox,但无法强制转换对象

C# 正在尝试将BigInt64项添加到listbox,但无法强制转换对象,c#,sql,asp.net,C#,Sql,Asp.net,我查看了其他帖子,尝试了一些建议,但无法解决这个问题。我必须为序列号设置一个bigint列,但不能在列表框中强制转换它们。以下是给我这个问题的代码行: ListBox.Items.Add(new ListItem((string)read2["IoTSerialNumber"])); I am getting this error: Unable to cast object of type 'System.Int64' to type'System.String'. 如前所述,我必

我查看了其他帖子,尝试了一些建议,但无法解决这个问题。我必须为序列号设置一个bigint列,但不能在列表框中强制转换它们。以下是给我这个问题的代码行:

 ListBox.Items.Add(new ListItem((string)read2["IoTSerialNumber"]));

 I am getting this error: Unable to cast object of type 'System.Int64' to 
 type'System.String'.
如前所述,我必须和bigint呆在一起

应该是:

ListBox.Items.Add(new ListItem(read2["IoTSerialNumber"].ToString()));
应该是:

ListBox.Items.Add(new ListItem(read2["IoTSerialNumber"].ToString()));

它抱怨不能用这种方式将Int64强制转换为字符串。 试试这个:

ListBox.Items.Add(new ListItem(read2["IoTSerialNumber"].ToString()));

编辑:以防万一:
ListBox.Items.Add
方法只接受字符串,这就是为什么您应该提供数字的字符串表示形式。

它抱怨您不能以这种方式将Int64强制转换为字符串。 试试这个:

ListBox.Items.Add(new ListItem(read2["IoTSerialNumber"].ToString()));

编辑:以防万一:
ListBox.Items.Add
方法只接受字符串,这就是为什么您应该提供数字的字符串表示形式。

就是这样做的!谢谢你,Pavel…我总是遇到字符串和int的问题,这很简单…非常感谢!NP:)还可以检查反向操作(从大数字到Int64的字符串):
Int64.TryParse(“这里有一些长数字,输出长结果)
(请记住,这样的内联声明需要C#7,否则您需要在之前声明
结果
)!谢谢你,Pavel…我总是遇到字符串和int的问题,这很简单…非常感谢!NP:)还可以检查反向操作(从大数字到Int64的字符串):
Int64.TryParse(“这里有一些长数字,输出长结果)
(请记住,这样的内联声明需要C#7,否则需要先声明
结果