lundi 22 juin 2020

System.InvalidOperationException: Invalid attempt to read when no data is present

I'm try to match data from two tables with different number of records to calculate average and store it. When i try to do it i get the following error

System.InvalidOperationException: Invalid attempt to read when no data is present. at

     try
        {
            con1.Open();
            string sq = "select venue_name from Feedback";
            string sq1 = "select vnname from Avrage";
            cmd1 = new SqlCommand(sq, con1);
            cmd2 = new SqlCommand(sq1, con1);
            SqlDataReader r1 = cmd1.ExecuteReader();
            SqlDataReader r2 = cmd2.ExecuteReader();
            i = 0;
            while (r2.HasRows)
            {
                j = 0;
                string na = r2.GetValue(i).ToString();
                while (r1.HasRows)
                {
                    string nm = r1.GetValue(j).ToString();
                    if (na.Equals(nm))
                   {
                        string sq2 = "select rate from Feedback";
                        cmd3 = new SqlCommand(sq2, con1);
                        SqlDataReader r3 = cmd2.ExecuteReader();
                        rate = float.Parse(r3.GetValue(0).ToString());
                        rate +=rate;
                        count++;
                    }
                }
                avrg = rate / count;
                //update avg
                Response.Write("Has successfully checked both tables.");
                i++;
            }
            r2.Close();
            r1.Close();
            con1.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Exception while checking.." + ex);
        }

Aucun commentaire:

Enregistrer un commentaire