Qun
Qun

Reputation: 11

Invalid attempt to call Read when reader is closed still not knowing what goes wrong

I looked around on stackoverflow but i can't really find the answer. Maybe I'm overlooking it. This is my code. after the first while it keeps going back to the while loop and after I close the reader it goes back to while(reader.read())

   public string VoegHoeveelheidToe(string HandmatigHoeveelheid, string ControleerBarcode)
      {
            string i = null;
            int Beginhoeveelheid;
            int ToeTeVoegenHoeveelheid;
            ToeTeVoegenHoeveelheid = Convert.ToInt32(HandmatigHoeveelheid);
            try
            {
                connectie.Open();
                OleDbDataReader reader = null;
                OleDbCommand command = new OleDbCommand("SELECT hoeveelheid from Voorraad,Product Where Voorraad.Barcode = Product.Barcode AND Voorraad.Barcode = '" + ControleerBarcode + "'", connectie);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Beginhoeveelheid = reader.GetInt32(0);
                    ToeTeVoegenHoeveelheid = Beginhoeveelheid + Convert.ToInt32(HandmatigHoeveelheid);
                    if(ToeTeVoegenHoeveelheid < Beginhoeveelheid)
                    {
                        i = Convert.ToString(3);
                    }
                    else
                    {
                        connectie.Close();
                        try
                        {
                            connectie.Open();
                            OleDbCommand command1 = new OleDbCommand();
                            command1.Connection = connectie;
                            string query = "Update Voorraad set hoeveelheid = " + ToeTeVoegenHoeveelheid + " Where barcode='" + ControleerBarcode + "'";
                            command1.CommandText = query;
                            command1.ExecuteNonQuery();
                            i = Convert.ToString(1);
                            connectie.Close();                                
                        }
                        catch (Exception)
                        {
                            i = Convert.ToString(2);
                        }

                    }
                }

            }
            catch (Exception )
            {
                i = Convert.ToString(2);
            }
            connectie.Close(); return i;

Upvotes: 0

Views: 77

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063619

You are closing the connection inside the while. This will implicitly close any readers, hence the error. Basically: don't do that. I strongly suggest adding using statements instead of closing and try/catch,finally, and to break out of a loop: break.

I am not sure why you have the connectie.Close(); inside the loop; perhaps you added that because of the "multiple active readers on a connection" issue. If this is the case, you will need to buffer information about what you want to do during the first reader, and only execute the pending commands when the reader is finished. Or use multiple connections.

Upvotes: 1

Related Questions