Reputation:
I have an sql command that depends on the results of other sql commands. There are five sql commands in this chain, but the problem occurs in the 4th.
The 5th command must save data to the archive table. When it has to run and take the completion_date value from the 4th command, it throws a null reference exception. Actually it says, that Reader4[0] can't be read because it has null value. That's wrong because it has value, because in the database these queries work fine and also because the condition if (Reader4.HasRows == true) is true, it means that WHERE statement is true in the 4th command, which also includes checking the completion_date! What's wrong with this 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();
Upvotes: 1
Views: 104
Reputation: 300719
Where are you startin to read from the reader
?
i.e. where do you call
Reader4.Read();
after
Reader4 = Command4.ExecuteReader();
Upvotes: 1