While loop

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 94
Reputation: Tank50 is an unknown quantity at this point 
Solved Threads: 1
Tank50's Avatar
Tank50 Tank50 is offline Offline
Junior Poster in Training

While loop

 
0
  #1
Mar 26th, 2009
Hi guys

I have a problem in my project.Below I mention the coding I wrote it for testing purpose.
In below coding the second while loop never run,only

            SqlDataReader reader = DbCon.Get_Invoices();
            int x = 0;
            while (reader.Read())
            {
                x++;                                                  1
             }
           MessageBox.Show(x.ToString());

           chart2.DataBindCrossTable(reader, "ExecutiveCode", "Date1", "Total", "");

           foreach (Series series in chart2.Series)
           {
               series.ChartType = SeriesChartType.Line;
           }
           chart2.ResetAutoValues();
           DataSet ds = new DataSet();
           DataTable dt = ds.Tables.Add("tbldata");
           int intIdx = 0;


           for (int i = 0; i < reader.FieldCount; i++)
           {
      dt.Columns.Add(reader.GetName(i), reader.GetFieldTypei));
           }

           while (reader.Read())
           {                                                      2
               x++;     }                                   
           MessageBox.Show(x.ToString());

         while (reader.Read())
          {                                                    3
          
          MessageBox.Show("test");
           DataRow r = dt.NewRow();
           if (intIdx >= intSkip)
           {
            for (int i = 0; i < reader.FieldCount; i++)
            {
          r[i] = reader[i];                                                                                
            }
      dt.Rows.Add(r);
         }


In above coding 3rd while loop never runs and it never give me Meassage I expected.1 and 2 while loop work and I generate the x value also.Please tell me y that 3rd loop never work

Thanks
Tank50
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 89
Reputation: TylerSBreton is an unknown quantity at this point 
Solved Threads: 3
TylerSBreton's Avatar
TylerSBreton TylerSBreton is offline Offline
Junior Poster in Training

Re: While loop

 
0
  #2
Mar 26th, 2009
Originally Posted by Tank50 View Post
Hi guys

I have a problem in my project.Below I mention the coding I wrote it for testing purpose.
In below coding the second while loop never run,only

            SqlDataReader reader = DbCon.Get_Invoices();
            int x = 0;
            while (reader.Read())
            {
                x++;                                                  1
             }
           MessageBox.Show(x.ToString());

           chart2.DataBindCrossTable(reader, "ExecutiveCode", "Date1", "Total", "");

           foreach (Series series in chart2.Series)
           {
               series.ChartType = SeriesChartType.Line;
           }
           chart2.ResetAutoValues();
           DataSet ds = new DataSet();
           DataTable dt = ds.Tables.Add("tbldata");
           int intIdx = 0;


           for (int i = 0; i < reader.FieldCount; i++)
           {
      dt.Columns.Add(reader.GetName(i), reader.GetFieldTypei));
           }

           while (reader.Read())
           {                                                      2
               x++;     }                                   
           MessageBox.Show(x.ToString());

         while (reader.Read())
          {                                                    3
          
          MessageBox.Show("test");
           DataRow r = dt.NewRow();
           if (intIdx >= intSkip)
           {
            for (int i = 0; i < reader.FieldCount; i++)
            {
          r[i] = reader[i];                                                                                
            }
      dt.Rows.Add(r);
         }


In above coding 3rd while loop never runs and it never give me Meassage I expected.1 and 2 while loop work and I generate the x value also.Please tell me y that 3rd loop never work

Thanks
Tank50
Reader has reached the end after the first while loop, and the "context" of it is basically EOF. Neither your second or third loop run. The only reason you think your second loop runs is because it is printing the correct value for x outside of the loop, which was calculated in the first loop. Try resetting reader before each loop.
Western New England College '08
Computer Science

Programming Lang's:

C, C#, Java, Lisp, MASM, Visual Basic 6
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 94
Reputation: Tank50 is an unknown quantity at this point 
Solved Threads: 1
Tank50's Avatar
Tank50 Tank50 is offline Offline
Junior Poster in Training

Re: While loop

 
0
  #3
Mar 27th, 2009
Hi

Yes your right,so how I reset sqldatareader?

Thanks
Tank50
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC