简体   繁体   English

System.InvalidCastException:“指定的演员表无效。” C# MYSQL

[英]System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL

I am trying to use this code to get the sum of amount from database:我正在尝试使用此代码从数据库中获取金额的总和:

MySqlCommand cmdsumpayment = new MySqlCommand("SELECT sum(amount) as'total' from payments where unit_id = " + UNITID + " AND deleted_at IS NULL", conn);
                        MySqlDataReader rdr2 = cmdsumpayment.ExecuteReader();
                        if (rdr2.HasRows) {
                            while (rdr2.Read())
                        {
                            TOTALPAYMENTS = rdr2.GetInt32("total");
                        }
                        }
                        else
                            MessageBox.Show("nothing found");

It works and I can get the sum(amount) from the payments table but my problem is if I search for something that does not have payments, I get this error:它有效,我可以从付款表中获取总和(金额),但我的问题是如果我搜索没有付款的东西,我会收到此错误:

System.InvalidCastException: 'Specified cast is not valid.

For example, if unit id = 1 and it does have payments, I can have the result in TOTALPAYMENTS but if it does not have any payments I get errors and the application crashed.例如,如果 unit id = 1 并且它确实有付款,我可以在 TOTALPAYMENTS 中得到结果,但如果它没有任何付款,我会收到错误并且应用程序崩溃。 can someone help me with how to handle the empty search without crashing?有人可以帮我处理空搜索而不崩溃吗? I also try if (rdr2.HasRows) but it does not work, I don't know why.我也尝试if (rdr2.HasRows)但它不起作用,我不知道为什么。

I am 100% sure that you have amount null in the table and then you are getting type cast while converting to integer.我 100% 确定您在表中有amount null,然后您在转换为 integer 时进行类型转换。 With the code you have posted you'll not get the type cast exception if there are no rows in the table for the given condition.使用您发布的代码,如果表中没有针对给定条件的行,您将不会获得类型转换异常。 So you can put breakpoint and debug or put some logs.所以你可以放断点和调试或放一些日志。 You can also try running the query directly into the database to see whether it's returning records or not.您也可以尝试将查询直接运行到数据库中,以查看它是否返回记录。

You can modify the query if amount is null.如果金额为 null,您可以修改查询。

SELECT sum(isnull(amount, 0)) as'total' from payments where unit_id = " + UNITID + " AND deleted_at IS NULL

And I would recommend to use parameterised query to avoid sql injection.我建议使用参数化查询来避免 sql 注入。

Edit : I have got the root cause of your issue.编辑:我已经找到了您问题的根本原因。 SqlDataReader.GetInt32 expects integer value and you are passing string value. SqlDataReader.GetInt32需要 integer 值,并且您正在传递字符串值。 So you need to pass 0 instead of “total” .所以你需要传递0而不是“total” As mentioned in the documentation you should call IsDBNull to check for null values before calling this method如文档中所述,您应该在调用此方法之前调用IsDBNull来检查 null 值

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

相关问题 C#System.InvalidCastException:'指定的转换无效。' 将MySQL与Discord.Net一起使用时 - C# System.InvalidCastException: 'Specified cast is not valid.' When using MySQL with Discord.Net System.InvalidCastException:'指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query System.InvalidCastException:指定的强制转换无效。错误 - System.InvalidCastException: Specified cast is not valid. Error Xamarin 形式:System.InvalidCastException:“指定的强制转换无效。” - Xamarin forms: System.InvalidCastException: 'Specified cast is not valid.' 在 xamarin 项目 System.InvalidCastException:“指定的演员表无效。” - in xamarin project System.InvalidCastException: 'Specified cast is not valid.' C#System.InvalidCastException:指定的转换无效 - C# System.InvalidCastException: Specified Cast is not valid 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM