I am running a C# windows application, and I get an error message when trying to use a datareader. The error message is:
"Invalid attempt to call CheckDataIsReady when reader is closed."
I used stop point and saw that the code works fine until it enters the "while" loop. Once inside, it gives the error message.
I have tried to do it without closing the previous reader, but then the message changed to something like "there is already an open reader" or some such.
Here's the code:
conn = new SqlConnection(DBConnectionString);
SqlCommand select_cmd = new SqlCommand("SELECT usrRealname, usrIsowner FROM tblUSERS WHERE usrNum = " + UserID, conn);
SqlCommand select_orders = new SqlCommand("SELECT orderNum, orderBy, orderShipadrs, orderDate, orderTotal FROM tblOrders WHERE orderDeliveryDate is NULL AND fkorderTakenbyusrnum = " + UserID, conn);
conn.Open();
SqlDataReader dr = select_cmd.ExecuteReader();
dr.Read();
CurrentUser User = new CurrentUser(Convert.ToString(dr[0]), UserID, Convert.ToBoolean(dr[1]));
DetailsLabel.Text = String.Format("Welcome {0}, ID number {1}. {2}", User.getname, UserID, User.getOwner);
dr.Close();
SqlDataReader orders = select_orders.ExecuteReader();
while (orders.Read())
{
UnfulfilledOrders CurrentOrder = new UnfulfilledOrders(Convert.ToInt32(dr[0]), Convert.ToString(dr[1]), Convert.ToString(dr[2]), Convert.ToString(dr[3]), Convert.ToInt32(dr[4]));
OrderList.Items.Add(CurrentOrder);
}
What I'd trying to do is add class (UnfulfilledOrders) type objects to a listbox (OrderList).
The thing that baffles me is that I used such a while loop in a previous form in the same app, and it worked fine there. I really have no idea what I'm doing wrong. I tried twiking the code, adding or removing certain parts, but nothing seems to work.