日期:2014-05-18  浏览次数:20574 次

ASP.NET的循环问题,有代码,请教下!
这是我写的一个订餐系统的统计页面的代码,我想通过for循环得到一个时间段内的某个部门的订餐数量TextBox_BookMorningCountTotal.Text,
其中一些代码,比如where.tostring()可以不用管,是我之前写的一些查询控制条件。
我的问题是在累加的问题上, //sumNoon+=sumNoon; 或 //sumMorning =sumMorning + sumMorning; 
我使用这种形式得到的结果有问题,所以想请教下应该怎么累加?急哦!
  DateTime t1 = Convert.ToDateTime(WebCalendar1.Text);
  DateTime t2 = Convert.ToDateTime(WebCalendar2.Text);
  TimeSpan ts = t2 - t1;
  int datecount = ts.Days;
  for (int i = 0; i <= datecount;i++ )
  {
  dt1 = this.WebCalendar1.Text;
  DateTime date = Convert.ToDateTime(dt1);
  date=date.AddDays(i);
  week =CommonFunction.Week(date);
  weekday = ((int)date.DayOfWeek ) % 6 ;
  string sql_sum = "";
  object obj;

  /* 获取员工订餐早餐订餐数量 */
  sql_sum =string.Format(
  @"Select Sum(BookMorningCheck{0}) AS BookMorningCheckTotal From Dinner Where" + where.ToString(), weekday.ToString()
  );
  obj =DbHelperSQL.GetSingle(sql_sum);
  int sumMorning= (obj != null) ? Convert.ToInt32(obj) : 0;
  //sumMorning =sumMorning + sumMorning; 

  this.TextBox_SQL.Text = this.TextBox_SQL.Text + "\n" + sql_sum;
  this.TextBox_BookMorningCountTotal.Text =sumMorning.ToString();

  /* 获取员工订餐中餐订餐数量 */
  sql_sum =string.Format(
  @"Select Sum(BookNoonCheck{0}) AS BookNoonCheckTotal From Dinner Where" + where.ToString(), weekday.ToString()
  );

  obj =DbHelperSQL.GetSingle(sql_sum);
  int sumNoon = (obj != null) ? Convert.ToInt32(obj) : 0;
  //sumNoon+=sumNoon;
  this.TextBox_SQL.Text = this.TextBox_SQL.Text + "\n" + sql_sum;
  this.TextBox_BookNoonCountTotal.Text = sumNoon.ToString();

  /* 获取员工订餐晚餐订餐数量 */
  sql_sum = string.Format(
  @"Select Sum(BookEveningCheck{0}) AS BookEveningCheckTotal
  From Dinner
  Where" + where.ToString(), weekday.ToString());
  obj = DbHelperSQL.GetSingle(sql_sum);
  int sumEvning = (obj != null) ? Convert.ToInt32(obj) : 0;
  //sumEvning =sumEvning +sumEvning;

  this.TextBox_SQL.Text = this.TextBox_SQL.Text + "\n" + sql_sum;
  this.TextBox_BookEveningCountTotal.Text = sumEvning.ToString();

  this.TextBox_All.Text = (sumMorning + sumNoon + sumEvning).ToString();
   

  }
 

------解决方案--------------------
C# code
     //应该在循环外面定义变量,循环内累加  

    DateTime t1 = Convert.ToDateTime(WebCalendar1.Text);
    DateTime t2 = Convert.ToDateTime(WebCalendar2.Text);
    TimeSpan ts = t2 - t1;
    //定义累加变量,初始化为0
    int sumMorning=0;
    int sumEvning=0;
    int sumNoon =0;
    int datecount = ts.Days;
    for (int i = 0; i <= datecount;i++ )
    {
      dt1 = this.WebCalendar1.Text;
      DateTime date = Convert.ToDateTime(dt1);