我选十条信息,点击提交按钮提交成功以后会把所选的信息和登陆时用户的工号写进数据表.private void Button1_Click(object sender, System.EventArgs e) 
{
int iChecked =0;  
int []temp=new int [10];    
for (int i=0; i <MyList.Items.Count; i++)  
{               
CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");   
if (remove.Checked == true)                  
{
temp[iChecked] = i;
iChecked++; 

             
}    
}
if (temp.Length==10)                    

Response.Write("<script>confirm('确认要提交吗?')</script>");   
}                      
else                   
{
MyError.Text = "您的输入有问题。";                                           
return;                   
}   //Label lblProductID = (Label) MyList.Items[i].FindControl("NewsID"); 
string sqldb=ConfigurationSettings.AppSettings["ConnectionString"];//连接本地计算机的                     
SqlConnection myConnection=new SqlConnection(sqldb);//创建带筛子的SQL语句                    
myConnection.Open ();

for (int i=0; i <MyList.Items.Count; i++)    
{
CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");     
if (remove.Checked == true) 
{
String strSQL = String.Format("insert into Ballot1(BallotID,UserdID) values ({0},{1})",temp[i],Session["Uid"]);
SqlCommand cmd = new SqlCommand(strSQL,myConnection);                     
cmd.ExecuteNonQuery(); 

}  
myConnection.Close(); }
我的问题是按顺序选了十条记录,就是我选前十条,
程序并未报错,但是如果我是不按顺序选的,程序会出现如下错误:
索引超出了数组界限。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.IndexOutOfRangeException: 索引超出了数组界限。源错误: 行 109: if (remove.Checked == true) 
行 110: {
行 111: String strSQL = String.Format("insert into Ballot1(BallotID,UserdID) values ({0},{1})",temp[i],Session["Uid"]);
行 112: SqlCommand cmd = new SqlCommand(strSQL,myConnection);                     
行 113: cmd.ExecuteNonQuery();  源文件: f:\be20\webroot\news\newsvote.aspx.cs    行: 111 堆栈跟踪: [IndexOutOfRangeException: 索引超出了数组界限。]
   News.Newsvote.Button1_Click(Object sender, EventArgs e) in f:\be20\webroot\news\newsvote.aspx.cs:111
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()

解决方案 »

  1.   

    String strSQL = String.Format("insert into Ballot1(BallotID,UserdID) values ({0},'{1}')",i,Session["Uid"]);for (int i=0; i <MyList.Items.Count; i++)  
    {               
    CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");   
    if (remove.Checked == true)                  
    {
    temp[iChecked] = i;
    iChecked++; 

                 
    }    
    }
    if (temp.Length==10)                    

    Response.Write("<script>confirm('确认要提交吗?')</script>");   
    }                      
    else                   
    {
    MyError.Text = "您的输入有问题。";                                           
    return;                   
    }
    这段程序干吗用的啊?temp的length永远是10,因为你int []temp=new int [10];
    个人认为可以去掉了
    如果你想判断是否选择10个,可以把int[]改成arraylist,然后用arraylist.Add
      

  2.   

    String strSQL = "";
    for(int i = 0;i<array.Count;i++)
    {
        strSQL += String.Format("insert into Ballot1(BallotID,UserdID) values ({0},'{1}')",array[i],Session["Uid"]);}
      

  3.   

    if (temp.Length==10)                    

    Response.Write("<script>confirm('确认要提交吗?')</script>");   
    }                      
    else                   
    {
    MyError.Text = "您的输入有问题。";                                           
    return;                   
    }
    我希望提交十条成功以后可以有个提示
      

  4.   

    去掉temp,我看不出它有什么用处
      

  5.   

    我是想先选出10信息,然后把挑选的10条信息存入数组temp里,然后取出来插入数据表里
      

  6.   

    private void Button1_Click(object sender, System.EventArgs e) 
    {
    // int iChecked =0;  
    ArrayList arrlist = new ArrayList();//定义数组  
    for (int i=0; i <MyList.Items.Count; i++)  
    {               
    CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");   
    Label lblProductID = (Label) MyList.Items[i].FindControl("NewsID"); 
    if (remove.Checked == true)                  
    {
    i++;
    arrlist.Add(i);
    i--;
    }
    }
    if (arrlist.Count<=10)                    

    Response.Write("<script>confirm('确认要提交吗?')</script>");   
    }    
                     
    else                   
    {
    MyError.Text = "您的输入有问题。";                                           
    return;                   
    }  
    string sqldb=ConfigurationSettings.AppSettings["ConnectionString"];//连接本地计算机的                     
    SqlConnection myConnection=new SqlConnection(sqldb);//创建带筛子的SQL语句                    
    myConnection.Open ();

    // for (int i=0; i <MyList.Items.Count; i++)    
    // {
    // CheckBox remove = (CheckBox) MyList.Items[i].FindControl("Remove");     
    // if (remove.Checked == true) 
    // {
    for(int j = 0;j<arrlist.Count;j++)
    {
    String strSQL = String.Format("insert into Ballot1(BallotID,UserdID) values ({0},{1})",arrlist[j],Session["Uid"]);
    SqlCommand cmd = new SqlCommand(strSQL,myConnection);                     
    cmd.ExecuteNonQuery(); 

    BindGrid();  

    myConnection.Close();
    // }
    }