指定演员表无效。在wpf c#? [英] Specified cast is not valid. in wpf c#?

查看:73
本文介绍了指定演员表无效。在wpf c#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int resourceType = row.Field<int>("jbd_jobno");



获取指定错误的演员无效。



jbd_jobno是一个整数



请帮我解决


getting an error specified cast is not valid.

"jbd_jobno" is an integer

Please help me out

推荐答案

请查看 SQL Server数据类型映射(ADO.NET) [ ^ ]并确保在c#中为数据库列类型选择了正确的类型。如果您的列支持空值,则需要使用可空类型,如下所示

Please check SQL Server Data Type Mappings (ADO.NET)[^] and make sure you have selected correct type in c# for your database column type. If your column support null values you need to use nullable type as below
int? resourceType = row.Field<int?>("jbd_jobno");


对解决方案1的一些澄清(请参阅我的评论):



请查看此扩展方法的声明:

http://msdn.microsoft.com/en-us/library/bb360891%28v=vs.110%29.aspx [ ^ ]。



对于某些数据行和某些列名,该方法尝试获取此行的属性值。即使这个语法本身是强类型,也没有奇迹这样的东西:属性的运行时类型可能是也可能不是赋值兼容使用您使用的实际泛型参数类型(在这种情况下为 int )。如果它不兼容,则输入类型失败,抛出您显示的异常。您需要为此调用使用正确的值类型,这本身是正确的。只需找出预期的正确类型。



-SA
Some clarification to Solution 1 (please see my comment to it):

Please look at the declaration of this extension method:
http://msdn.microsoft.com/en-us/library/bb360891%28v=vs.110%29.aspx[^].

For some data row and some column name, the method attempts to get the attribute value for this row. Even though this syntax itself is strongly typed, there is no such thing as miracle: the runtime type for the attribute may or may not be assignment-compatible with the actual generic parameter type you use (int, in this case). If it is not compatible, type cast is failed, throws exception you show. You need to use correct value type for this call, which is itself correct. Just find out what is the correct type expected.

—SA


这篇关于指定演员表无效。在wpf c#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆