954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

dr.Read()

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();



                }
            


            }     


            }
wissam.ashkar
Newbie Poster
18 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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.

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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

wissam.ashkar
Newbie Poster
18 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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.

hericles
Practically a Posting Shark
823 posts since Nov 2007
Reputation Points: 136
Solved Threads: 167
 

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

mani-hellboy
Junior Poster in Training
69 posts since Feb 2012
Reputation Points: 0
Solved Threads: 7
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You