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

While loop

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++;                                                 <strong> 1</strong>
             }
           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())
           {                                                      <strong>2</strong>
               x++;     }                                   
           MessageBox.Show(x.ToString());

         while (reader.Read())
          {                                                    <strong>3</strong>
          
          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

Tank50
Junior Poster
124 posts since Aug 2008
Reputation Points: 12
Solved Threads: 1
 

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++;                                                 <strong> 1</strong>
             }
           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())
           {                                                      <strong>2</strong>
               x++;     }                                   
           MessageBox.Show(x.ToString());

         while (reader.Read())
          {                                                    <strong>3</strong>
          
          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.

TylerSBreton
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 25
Solved Threads: 3
 

Hi

Yes your right,so how I reset sqldatareader?

Thanks
Tank50

Tank50
Junior Poster
124 posts since Aug 2008
Reputation Points: 12
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You