简体   繁体   English

C#System.InvalidCastException:指定的转换无效

[英]C# System.InvalidCastException: Specified Cast is not valid

I've been struggling with one problem these past few hours. 在过去的几个小时中,我一直在努力解决一个问题。

I keep getting an error and it tells me that the specified cast is not valid. 我不断收到错误消息,它告诉我指定的转换无效。 I'm able to change the values on the database using the stored procedure. 我可以使用存储过程更改数据库上的值。 However, when I try doing it with the executable I get that error. 但是,当我尝试使用可执行文件执行此操作时,出现该错误。

This is the error 这是错误

System.InvalidCastException: Specified cast is not valid.
at Administrator_Panel.DB.ReadAccountInfo(String UserID, In32& Count)

This is the code. 这是代码。

 public static Account ReadAccountInfo(string UserID, out int Count)

 if (Reader.Read())
                ReturnValue = new Account((int)Reader["UserUID"],
                                            (string)Reader["Pw"],
                                            (bool)Reader["Admin"],
                                            (bool)Reader["Staff"],
                                            (short)Reader["Status"],
                                            (int)Reader["Point"],
                                            (int)Reader["DaemonPoints"]);

Any help would be appreciated. 任何帮助,将不胜感激。 :) Thank you :) 谢谢

See this answer: How to (efficiently) convert (cast?) a SqlDataReader field to its corresponding c# type? 看到这个答案: 如何将SQLDataReader字段(有效地)转换(广播?)为其对应的C#类型?

You can't just cast SqlDataReader fields to their corresponding value types because of the possibility of nulls. 由于存在空值的可能性,因此不能仅将SqlDataReader字段转换为它们的相应值类型。 You can use nullable types but it's likely your Account object isn't setup to take nullable types. 您可以使用可为空的类型,但很可能您的Account对象未设置为采用可为空的类型。

One way you can try to handle this is to add null checking: 您可以尝试处理此问题的一种方法是添加null检查:

 ReturnValue = new Account(Reader["UserUID"] == DBNull.Value ? 0 : (int)Reader["UserUID"] ,
                           Reader["Pw"] == DBNull.Value ? "" : Reader["Pw"].ToString(),
                           Reader["Admin"] == DBNull.Value ? false : (bool)Reader["Admin"],
                           Reader["Staff"] == DBNull.Value ? false : (bool)Reader["Staff"],
                           Reader["Status"] == DBNull.Value ? (short) 0 : (short)Reader["Status"],
                           Reader["Point"] == DBNull.Value ? 0 :  (int)Reader["Point"],
                           Reader["DaemonPoints"] == DBNull.Value ? 0 : (int)Reader["DaemonPoints"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL System.InvalidCastException:指定的强制转换无效的C# - System.InvalidCastException: Specified cast is not valid C# 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时 - C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net C#请求处理程序错误System.InvalidCastException:指定的强制转换无效 - C# REQUEST HANDLER ERROR System.InvalidCastException: Specified cast is not valid System.InvalidCastException:使用postgres datareader在C#中指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid in C# using postgres datareader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM