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

帮我看个小错误!都来都来,立即给分!!
索引超出范围。必须为非负值并小于集合大小
参数名:   index  
说明:   执行当前   Web   请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。  

异常详细信息:   System.ArgumentOutOfRangeException:   索引超出范围。必须为非负值并小于集合大小。
参数名:   index

源错误:  


行   170:                         if   (chk.Checked)
行   171:                         {
行   172:                                 ID[i]   =   Convert.ToInt32(list1.DataKeys[dl.ItemIndex+1].ToString());
行   173:                                 try
行   174:                                 {
 


我的代码如下:
protected   void   ImageButton1_Click(object   sender,   ImageClickEventArgs   e)
        {
                int[]   ID   =   new   int[list1.Items.Count];
                int   i   =   0;

                foreach   (DataListItem   dl   in   list1.Items)
                {
                        CheckBox   chk   =   (CheckBox)dl.FindControl( "chkSelect ");
                        if   (chk.Checked)
                        {
                                ID[i]   =   Convert.ToInt32(list1.DataKeys[dl.ItemIndex].ToString());
                                try
                                {
                                        conn   =   new   SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "ConnStr "]);
                                        conn.Open();
                                        cmd   =   new   SqlCommand( "delete   from   zhanquxinxi   where   zh=ID[i] ",   conn);
                                        cmd.ExecuteNonQuery();
     
                                }
                &nbs