string sql = "select *,Age*amount As Count from Cart where ID='" + i + "'";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        da.Fill(ds);
        float fl_Count = 0;
        foreach(DataRow dr in ds.Tables[0].Rows)
        {
            fl_Count += Convert.ToSingle(dr[7]);
        }
        Age_Count = fl_Count.ToString();
        dlShoppingCart.DataSource = ds;
        dlShoppingCart.DataBind();
 fl_Count += Convert.ToSingle(dr[7]);
显示无法找到dr[7],这个是怎么回事

解决方案 »

  1.   

    数组如果用foreach循环,无法输入确定的元素把foreach改为for吧
    foreach(DataRow dr in ds.Tables[0].Rows) 
            { 
                fl_Count += Convert.ToSingle(dr[7]); 
            } for(count=0;count<ds.Tables[0].Rows;count++) 
            { 
                if(count==7)
                fl_Count += Convert.ToSingle(ds.Tables[0].Rows[7][列名].ToString()); 
            } 
      

  2.   

    for(count=0;count <ds.Tables[0].Rows.Count;count++) 
            { 
                if(count==7) 
                {
                     fl_Count += Convert.ToSingle(ds.Tables[0].Rows[7][列名].ToString()); 
                }
            }
      

  3.   

    最少要有8个字段,才能找到值
    另:最好用字段名代替里面的数字,比如:dr["amount"]
      

  4.   

    string sql = "select *,Age*amount As Count from Cart where ID='" + i + "'";你ID是字符型吗还有你调试一下看一下就知道总共的行数是多少了
      

  5.   

    foreach是取只读的,在取的时候数据不能变(包括修改,删除,添加等)。要避免这个问题,就应该使用for循环。