943,907 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 4125
  • C# RSS
Mar 26th, 2009
0

While loop

Expand 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
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
Tank50 is offline Offline
124 posts
since Aug 2008
Mar 26th, 2009
0

Re: While loop

Click to Expand / Collapse  Quote originally posted by Tank50 ...
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.
Reputation Points: 25
Solved Threads: 3
Junior Poster in Training
TylerSBreton is offline Offline
89 posts
since Oct 2006
Mar 27th, 2009
0

Re: While loop

Hi

Yes your right,so how I reset sqldatareader?

Thanks
Tank50
Reputation Points: 12
Solved Threads: 1
Junior Poster
Tank50 is offline Offline
124 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: DataGridView not reflecting changes to DataTable
Next Thread in C# Forum Timeline: finger print simulator in c#





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC