Hello, I get this error: "No data exists for the row/column." at the red section in my code. I am sure my database row i am selecting has data in dr.get..(3) and dr.get...(5)

private void button1_Click(object sender, EventArgs e)
        {
            int cpnumber = Convert.ToInt32(LastCP.Text);
            DateTime cpdate = Convert.ToDateTime(dateTimePicker1.Text);
            double position = Convert.ToDouble(PositionTB.Text);
            double totalsc = Convert.ToDouble(TscoreTB.Text);  
            double military = Convert.ToDouble(MilitaryTB.Text);
            decimal RP = Convert.ToDecimal(RPhourTB.Text);

            cmd = null;
            cmd = new OleDbCommand();
            MyConnection.OpenMyConnection();
            cmd.CommandText = "SELECT * FROM checkpoint WHERE ika_checkpoint_number = 1";

            cmd.Connection = MyConnection.con;
            OleDbDataReader st = cmd.ExecuteReader();

            if (st.Read())
            {

                cmd = null;
                cmd = new OleDbCommand();
                
                cmd.CommandText = "Select * from checkpoint where ID=(select max(ID) from checkpoint)";

                cmd.Connection = MyConnection.con;
                OleDbDataReader dr = cmd.ExecuteReader();

                double lastposition = Convert.ToDouble(dr.GetDouble(3));
                double lastscore = Convert.ToDouble(dr.GetDouble(5));

                double poschange = position - lastposition;
                double scorechange = totalsc - lastscore;

                cmd = null;
                cmd = new OleDbCommand();
                cmd.Connection = MyConnection.con;
                
                cmd.CommandText = "INSERT INTO checkpoint ([ika_checkpoint_number], [ika_checkpoint_date], [ika_position], [ika_position_change], [ika_total_score], [ika_score_growth], [ika_military], [ika_research]) VALUES (@cpnum, @cpdate, @pos, @poschange, @cpscore, @cpscorechange, @cpmilitary, @cprp)";
                cmd.Parameters.AddRange(new OleDbParameter[]
                            {
                               new OleDbParameter("@cpnum", cpnumber),
                               new OleDbParameter("@cpdate", cpdate),
                               new OleDbParameter("@pos", position),
                new OleDbParameter("@poschange", poschange),
                new OleDbParameter("@cpscore", totalsc),
                new OleDbParameter("@cpscorechange", scorechange),
                new OleDbParameter("@cpmilitary", military),
                new OleDbParameter("@cprp", RP),
                                     
                             });
                int count = cmd.ExecuteNonQuery();
                MyConnection.CloseMyConnection();
                if (count > 0)
                {
                    this.Hide();
                    IkariamDataForm newform = (IkariamDataForm)Application.OpenForms["IkariamDataForm"];
                    newform.FillAchievementsGrid();
                    newform.FillCheckPointGrid(); ;
                    this.Close();



                }
            


            }     


            }

First, why are you calling the Convert.ToDouble when you are getting it as a double? It's not needed.

Second, if it says there is no data, there is no data. Put a breakpoint on the line and inspect the values and see if it is what you wanted.

Yeah there is no need to convert it to double. Please provide me with the code of the breakpoint

There is no code to create a breakpoint. In the VS IDE click in the left margin beside the line you want to check (a red dot will appear) and then start your app in debug mode. The code will run until it reaches that point and then open up the various debug windows. The vaules of your variables will be shown.
Check the values in the dataReader.

simply use try and catch

i think u get db null exception. if u got this error use isnull() method and replace that null value to double you can easily get what you want

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.