简体   繁体   English

数据为空。 无法在Null值上调用此方法或属性(mysql至文本框)

[英]Data is Null. This method or property cannot be called on Null value (mysql to textbox)

My friend wants to transfer her data from the database to a textbox to her program in c# but it gets an error of this: 我的朋友想将她的数据从数据库传输到文本框,然后用c#传输到她的程序,但是出现了以下错误:

"Data is Null. This method or property cannot be called on Null values." “数据为空。不能在空值上调用此方法或属性。”

Here is the code by the way: 下面是代码:

sql_command = new MySqlCommand("select sum(lt_min_hours) as TotalLate from tbl_late where (late_date between '" + date_start + "' and '" + date_to + "' and empid = '" + empid + "')", sql_connect);
sql_reader = sql_command.ExecuteReader();            

if (sql_reader.Read())
{
    textBox_tlate.Text = sql_reader.GetString("TotalLate");
}
else
{
    MessageBox.Show("No Data.");
}

From documentation ; 文件 ;

SUM() returns NULL if there were no matching rows. 如果没有匹配的行, SUM()将返回NULL

But first of all, You should always use parameterized queries . 但首先,您应该始终使用参数化查询 This kind of string concatenations are open for SQL Injection attacks. 这类字符串连接对SQL注入攻击开放。

After that, you can use ExecuteScalar instead of ExecuteReader which is returns only one column with one row. 之后,您可以使用ExecuteScalar而不是ExecuteReader ,后者仅返回一列和一行。 In your case, this is exactly what you want. 就您而言,这正是您想要的。

textBox_tlate.Text = sql_command.ExecuteScalar().ToString();

Also use using statement to dispose your connection and command automatically. 也可以使用using语句自动配置您的连接和命令。

You need to test for DbNull.Value against your field before assigning it to the textbox 您需要先针对您的字段测试DbNull.Value,然后再将其分配给文本框

if (sql_reader.Read())
{
    if(!sql_reader.IsDbNull(sql_reader.GetOrdinal("TotalLate")))
        textBox_tlate.Text = sql_reader.GetString("TotalLate");
}

EDIT 编辑
According to your comment, nothing happens, so the WHERE condition fails to retrieve any record and the result is a NULL. 根据您的评论,什么都不会发生,因此WHERE条件无法检索任何记录,结果为NULL。
Looking at your query I suppose that your variables containing dates are converted to a string in an invalid format. 查看您的查询,我想您的包含日期的变量将转换为无效格式的字符串。 This could be fixed using a ToString and a proper format string (IE: yyyy-MM-dd) but the correct way to handle this is through a parameterized query 可以使用ToString和正确的格式字符串(即yyyy-MM-dd)解决此问题,但正确的处理方法是通过参数化查询

sql_command = new MySqlCommand(@"select sum(lt_min_hours) as TotalLate 
                 from tbl_late 
                 where (late_date between @init and @end and empid = @id", sql_connect);
sql_command.Parameters.Add("@init", MySqlDbType.Date).Value = date_start;
sql_command.Parameters.Add("@end", MySqlDbType.Date).Value = date_end;
sql_command.Parameters.Add("@id", MySqlDbType.Int32).Value = empid;
sql_reader = sql_command.ExecuteReader();            

This assumes that date_start and date_end are DateTime variables and empid is an integer one. 假设date_startdate_end是DateTime变量,而empid是整数1。 In this way, the parsing of the parameters is done by the MySql engine that knows how to handle a DateTime variable. 这样,参数解析是由知道如何处理DateTime变量的MySql引擎完成的。 Instead your code uses the automatic conversion made by the Net Framework that, by default, uses your locale settings to convert a date to a string. 相反,您的代码使用Net Framework进行的自动转换,默认情况下,该转换使用区域设置将日期转换为字符串。

暂无
暂无

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

相关问题 数据为空。 无法在Null值错误时调用此方法或属性 - Data is Null. This method or property cannot be called on Null values error 数据为空。 不能对 Null 值调用此方法或属性 - Data is Null. This method or property cannot be called on Null values 数据为空。 在C#.net中联接两个表后,不能在空值上调用此方法或属性 - Data is Null. This method or property cannot be called on null value after joining two tables in C#.net System.Data.SqlTypes.SqlNullValueException:数据为 Null。 无法对 Null 值调用此方法或属性 - System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values EF 生成 System.Data.SqlTypes.SqlNullValueException: 数据为 Null。无法对 Null 值调用此方法或属性 - EF generate System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values 修复“数据为 Null。如果不使用数据注释,则无法在 Null 值上调用此方法或属性”, - fix " Data is Null. This method or property cannot be called on Null values " without using data annotations , SQL Server引发错误:数据为空。 不能在Null值上调用此方法或属性 - SQL Server is throwing an error: Data is Null. This method or property cannot be called on Null values EF Core 6:数据为 Null。 无法对 Null 值调用此方法或属性 - EF Core 6: Data is Null. This method or property cannot be called on Null values 升级到 .NET 6,出现 EF 问题 - SqlNullValueException:'数据为 Null。 不能对 Null 值调用此方法或属性。 - Upgraded to .NET 6, having issues with EF - SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.' 数据为空。 不能在空值上调用此方法或属性。(使用组合框) - Data is Null. This method or property cannot be called on null values.(using combo box)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM