指定的类型转换是无效的
本文关键字:无效 类型转换 | 更新日期: 2023-09-27 18:02:59
我正在尝试求和列WHERE date BETWEEN @from AND @to,如下所示
DateTime from = DateTime.Parse(dt_From.Value.ToShortDateString());
DateTime to = DateTime.Parse(dt_To.Value.ToShortDateString());
SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.ConnectionString);
SQLiteCommand cmd = null;
con.Open();
//Total Items Sold
string query = " SELECT SUM(total) FROM acc_items_income WHERE date BETWEEN @from AND @to AND deleted = 0 ";
cmd = new SQLiteCommand(query, con);
cmd.Parameters.Add(new SQLiteParameter("@from", from));
cmd.Parameters.Add(new SQLiteParameter("@to", to));
我正在试着读它,如图
SQLiteDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
double soldItems = dr.GetDouble(0);
txtTotalSoldItems.Text = soldItems.ToString("c", naira);
}
我的错误现在是,当我从行中选择datetime不在数据库中,或者不能SUM一个没有值的列,然后我得到这个错误。
指定的类型转换无效
在尝试读取列之前需要检查DBNull值。
while (dr.Read())
{
if (dr.IsDBNull(0)) { //Check for a null value at the 0 index
soldItems = 0;
} else {
soldItems = dr.GetDouble(0);
}
}