Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
error- There is no row at position 35.


What I have tried:

try
      {
          DataTable dt = new DataTable();
          //string str = "select round(Avg(Score),2) as Score from oeunitscore";
          string str = @"select om.unitgroup, round(Avg(Score),2) as Score, date(timestamp) as TimeStamp  from oeunitscore os, oeunitmaster om
                              where os.unitcode=om.unitcode and date(timestamp) BETWEEN SYSDATE() - interval 11 day AND SYSDATE()
                              group by  unitgroup, date(timestamp)
                              order by date(timestamp) , unitgroup asc";
          if (con.State == ConnectionState.Closed)
          {
              con.Open();
          }
          MySqlDataAdapter da = new MySqlDataAdapter(str, con);
          da.Fill(dt);
          if (dt.Rows.Count > 0)
          {
              date1 = Convert.ToDateTime(dt.Rows[0]["TimeStamp"]).ToString("dd-MM-yy");
              date2 = Convert.ToDateTime(dt.Rows[7]["TimeStamp"]).ToString("dd-MM-yy");
              date3 = Convert.ToDateTime(dt.Rows[14]["TimeStamp"]).ToString("dd-MM-yy");
              date4 = Convert.ToDateTime(dt.Rows[21]["TimeStamp"]).ToString("dd-MM-yy");
              date5 = Convert.ToDateTime(dt.Rows[28]["TimeStamp"]).ToString("dd-MM-yy");
              date6 = Convert.ToDateTime(dt.Rows[35]["TimeStamp"]).ToString("dd-MM-yy");
              date7 = Convert.ToDateTime(dt.Rows[42]["TimeStamp"]).ToString("dd-MM-yy");

              dh1 = Convert.ToSingle(dt.Rows[0]["Score"].ToString());
              di1 = Convert.ToSingle(dt.Rows[1]["Score"].ToString());
              fr1 = Convert.ToSingle(dt.Rows[2]["Score"].ToString());
              gf1 = Convert.ToSingle(dt.Rows[3]["Score"].ToString());
              lr1 = Convert.ToSingle(dt.Rows[4]["Score"].ToString());
              of1 = Convert.ToSingle(dt.Rows[5]["Score"].ToString());
              ut1 = Convert.ToSingle(dt.Rows[6]["Score"].ToString());

              dh2 = Convert.ToSingle(dt.Rows[7]["Score"].ToString());
              di2 = Convert.ToSingle(dt.Rows[8]["Score"].ToString());
              fr2 = Convert.ToSingle(dt.Rows[9]["Score"].ToString());
              gf2 = Convert.ToSingle(dt.Rows[10]["Score"].ToString());
              lr2 = Convert.ToSingle(dt.Rows[11]["Score"].ToString());
              of2 = Convert.ToSingle(dt.Rows[12]["Score"].ToString());
              ut2 = Convert.ToSingle(dt.Rows[13]["Score"].ToString());

              dh3 = Convert.ToSingle(dt.Rows[14]["Score"].ToString());
              di3 = Convert.ToSingle(dt.Rows[15]["Score"].ToString());
              fr3 = Convert.ToSingle(dt.Rows[16]["Score"].ToString());
              gf3 = Convert.ToSingle(dt.Rows[17]["Score"].ToString());
              lr3 = Convert.ToSingle(dt.Rows[18]["Score"].ToString());
              of3 = Convert.ToSingle(dt.Rows[19]["Score"].ToString());
              ut3 = Convert.ToSingle(dt.Rows[20]["Score"].ToString());

              dh4 = Convert.ToSingle(dt.Rows[21]["Score"].ToString());
              di4 = Convert.ToSingle(dt.Rows[22]["Score"].ToString());
              fr4 = Convert.ToSingle(dt.Rows[23]["Score"].ToString());
              gf4 = Convert.ToSingle(dt.Rows[24]["Score"].ToString());
              lr4 = Convert.ToSingle(dt.Rows[25]["Score"].ToString());
              of4 = Convert.ToSingle(dt.Rows[26]["Score"].ToString());
              ut4 = Convert.ToSingle(dt.Rows[27]["Score"].ToString());

              dh5 = Convert.ToSingle(dt.Rows[28]["Score"].ToString());
              di5 = Convert.ToSingle(dt.Rows[29]["Score"].ToString());
              fr5 = Convert.ToSingle(dt.Rows[30]["Score"].ToString());
              gf5 = Convert.ToSingle(dt.Rows[31]["Score"].ToString());
              lr5 = Convert.ToSingle(dt.Rows[32]["Score"].ToString());
              of5 = Convert.ToSingle(dt.Rows[33]["Score"].ToString());
              ut5 = Convert.ToSingle(dt.Rows[34]["Score"].ToString());

              dh6 = Convert.ToSingle(dt.Rows[35]["Score"].ToString());
              di6 = Convert.ToSingle(dt.Rows[36]["Score"].ToString());
              fr6 = Convert.ToSingle(dt.Rows[37]["Score"].ToString());
              gf6 = Convert.ToSingle(dt.Rows[38]["Score"].ToString());
              lr6 = Convert.ToSingle(dt.Rows[39]["Score"].ToString());
              of6 = Convert.ToSingle(dt.Rows[40]["Score"].ToString());
Posted
Updated 3-Oct-18 21:58pm
Comments
Herman<T>.Instance 4-Oct-18 3:46am    
Your table has less than 35 rows. That is what is is telling you.

1 solution

The error message could not be more clear:
There is no row at position 35.
Your code uses explicit row numbers;
date1 = Convert.ToDateTime(dt.Rows[0]["TimeStamp"]).ToString("dd-MM-yy");
date2 = Convert.ToDateTime(dt.Rows[7]["TimeStamp"]).ToString("dd-MM-yy");
date3 = Convert.ToDateTime(dt.Rows[14]["TimeStamp"]).ToString("dd-MM-yy");
date4 = Convert.ToDateTime(dt.Rows[21]["TimeStamp"]).ToString("dd-MM-yy");
date5 = Convert.ToDateTime(dt.Rows[28]["TimeStamp"]).ToString("dd-MM-yy");
date6 = Convert.ToDateTime(dt.Rows[35]["TimeStamp"]).ToString("dd-MM-yy");
date7 = Convert.ToDateTime(dt.Rows[42]["TimeStamp"]).ToString("dd-MM-yy");
without checking in any way that there are sufficient results returned to accommodate them: you need at least 43 rows returned in order for that to work.

Why don't you get them? We can't tell, because we have no access to your data or to your results.
Start by using the debugger to examine the actual results you get back from your query, and find out how many there are, and then check your data to see how many you were expecting.
Then add code to check you have the expected number of rows returned, and report a problem if not.

And do yourself a favour: stop using "magic numbers" to select your rows: it's not at all obvious why you want rows in sevens, but probably it's some kind of daily data and you want Sunday only or similar. But use something (a constant perhaps?) that reflects the usage, not 7, 14, 21, 28 , ... that tells nobody anything!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900