SQLdatareader error

admin

Administrator
Staff member
I'm reading an SQL. When the SQL has no records returned it has to reda an other SQL. This has to continue untill he finds a record.
myReaderTU2 works great. And when it returns no records, myReaderTU3 has to start. This works fine the first time he executes the While (myReaderTU3.Read()). But the second time he doesn't execute the my read. He jumps over the while? Do i need to close some extra's with this reader or why doesn't the while work the second time? thx a lot. Below you can find the code. Don't pay attention to the SQL statements, they work

SqlDataReader myReaderTU2 = null;
SqlCommand myCommandTU2 = new SqlCommand("SELECT tbClassifications.class_id, tbClassifications.class_parent, tbClassificationTexts.text_text, tbProducts.prod_number, tbClassificationTexts.text_fk_lang_id, tbClassificationTexts.text_sequence FROM tbProducts INNER JOIN ((tbClassifications INNER JOIN tbClassificationTexts ON tbClassifications.class_id = tbClassificationTexts.text_fk_class_id) INNER JOIN tbProductClassification ON tbClassifications.class_id = tbProductClassification.clprod_fk_class_id) ON tbProducts.prod_id = tbProductClassification.clprod_fk_prod_id WHERE (((tbClassificationTexts.text_fk_lang_id)=1) AND ((tbClassificationTexts.text_sequence)=15) AND ((tbClassifications.class_id)=" + parentID + "))", myConnectionTU1);
myReaderTU2 = myCommandTU2.ExecuteReader();
if (myReaderTU2.RecordsAffected == -1)
{
myReaderTU2.Close();
SqlDataReader myReaderTU3 = null;
SqlCommand myCommandTU3 = new SqlCommand("SELECT tbClassifications.class_id, tbClassifications.class_parent, tbClassificationTexts.text_text, tbProducts.prod_number, tbClassificationTexts.text_fk_lang_id, tbClassificationTexts.text_sequence FROM tbProducts INNER JOIN ((tbClassifications INNER JOIN tbClassificationTexts ON tbClassifications.class_id = tbClassificationTexts.text_fk_class_id) INNER JOIN tbProductClassification ON tbClassifications.class_id = tbProductClassification.clprod_fk_class_id) ON tbProducts.prod_id = tbProductClassification.clprod_fk_prod_id WHERE (((tbClassificationTexts.text_fk_lang_id)=1) AND ((tbClassifications.class_id)=" + parentID + "))", myConnectionTU1);
myReaderTU3 = myCommandTU3.ExecuteReader();
while (myReaderTU3.Read())
{
parentID = myReaderTU3["class_parent"].ToString();
break;
}
myReaderTU3.Close();
}Huh? You might want to reword things if you want the answer you're looking for, as the question is unclear.

A couple suggested changes to make things clearer and more consistent in the code (I usually do VB, not C#, so I may be slightly off on syntax):
-- Change -> if (myReaderTU2.RecordsAffected == -1)
to -> if (!myReaderTU2.Read())
-- Instead of this "loop" that you break out of without actually looping -> while (myReaderTU3.Read())
{
parentID = myReaderTU3["class_parent"].ToString();
break;
}
use an "if" instead -> if (myReaderTU3.Read())
{ parentID = myReaderTU3["class_parent"].ToString(); }
 
Top