How can I run SqlCommand inside while(dr.read())

Lumpkine

New Member
I'm getting an error:\[quote\] There is already an open DataReader associated with this Command which must be closed first.\[/quote\]My code:\[code\]SqlCommand cmd = new SqlCommand("SELECT * FROM shoppingcart", con);SqlDataReader dr;con.Open();dr = cmd.ExecuteReader();while (dr.Read()){ found = true; productdate = Convert.ToDateTime(dr.GetString(4)); string ago1 = string.Format("{0:MM/dd/yy}", Convert.ToString(productdate)); string productdate1 = string.Format("{0:MM/dd/yy}", Convert.ToString(ago)); int productqty = Convert.ToInt32( dr.GetValue(3)); int productID = Convert.ToInt32(dr.GetValue(2)); int cartID = Convert.ToInt32(dr.GetValue(0)); if (productdate < ago) { SqlCommand updateproducts = new SqlCommand("UPDATE products SET ProductQuantity=ProductQuantity+@ProductQuantity WHERE ProductID=@ProductID", con); updateproducts.Parameters.AddWithValue("@ProductQuantity", productqty); updateproducts.Parameters.AddWithValue("@ProductID", productID); updateproducts.ExecuteNonQuery(); SqlCommand deletecart = new SqlCommand("DELETE FROM shoppingcart WHERE ID=@ID", con); deletecart.Parameters.AddWithValue("@ID", cartID); deletecart.ExecuteNonQuery(); }}con.Close();\[/code\]I need to execute update and delete inside \[code\]while(dr.read())\[/code\] but that error pop up when I try to close connection then open again the while will stop and error will appear it said the \[code\]ExecuteReader\[/code\] is close. please help me.
 
Back
Top