我的asp.net应用程序遇到了一个奇怪的问题。简而言之,我有一个sql命令,它取决于其他sql命令的结果。此链中有五个sql命令,但问题出现在第4个。
第5个命令必须将数据保存到存档表。当必须运行并从第4个命令获取 completion_date 值时,它会抛出空引用异常。实际上它说, Reader4 [0] 无法读取,因为它具有空值。这是错误的,因为它有价值,因为在数据库中这些查询工作正常,并且因为条件 if(Reader4.HasRows == true)为真,这意味着 WHERE 声明在第4个命令中为true,其中还包括检查 completion_date !这个asp.net有什么问题?
Command4 = new SqlCommand("SELECT trade_date FROM Schedule WHERE traider_id=@traiderID AND good_id=@goodID AND position_id=@positionID AND status_id=1 AND completion_date IS NOT NULL", Connection4); //note the completion_date check
Command4.Parameters.Add("@traiderID", Convert.ToInt32(Reader3[0]));
Command4.Parameters.Add("@positionID", CurrentPosition);
Command4.Parameters.Add("@goodID", Convert.ToInt32(Reader1[0]));
Reader4 = Command4.ExecuteReader();
if (Reader4.HasRows == true) //this check is done successfully
{
Command5 = new SqlCommand("INSERT INTO Archive (traider_id, good_id, completion_date) VALUES (@traiderID, @goodID, @completionDate)", Connection5);
Command5.Parameters.Add("@traiderID", Convert.ToInt32(Reader3[0]));
Command5.Parameters.Add("@goodID", Convert.ToInt32(Reader1[0]));
Command5.Parameters.Add("@completionDate", Convert.ToDateTime(Reader4[0])); //Here is the problem
Command5.ExecuteNonQuery();
}
Reader4.Close();
任何想法,伙计们?我一直试图找出问题所在。这段代码是唯一可能出错的地方,但Reader4的值如何变为null?它在表中不为null,即使在执行Command4时它也不为空!
答案 0 :(得分:2)
您从哪里开始阅读reader
?
即。你在哪里打电话
Reader4.Read();
后
Reader4 = Command4.ExecuteReader();