[英]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()
returnsNULL
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_start
和date_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.