调试foreach语句时IndexOutOfBoundsException

本文关键字:IndexOutOfBoundsException 语句 foreach 调试 | 更新日期: 2023-09-27 18:28:16

//我已经列出了下面的代码,请看一看,如果我能得到一些帮助,我将不胜感激。我的程序在字符串studadmin=admin[g]行抛出索引越界异常;

protected void Button1_Click(object sender, EventArgs e)
{
    //retrieve studentdetails using List<String[];
    List<String[]> stud = new List<String[]>();         
    int i = 0;
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox check = (CheckBox)row.FindControl("CheckBox1");
        if (check.Checked)
        {
            String [] studDetail = new String [1];
            studDetail[0] = row.Cells[1].Text;
            stud.Add(studDetail);
        }
        i++;
    }
    int g = 0;
    foreach (String[] admin in stud)
    {
        String studadmin = admin[g]; 
        // here's whr the error are prompted (IndexOutOfBoundsException),
        // when it reads the following "admin", the loop just ends here with an error;         
            try
            {
                myConnection = db.getConnection();
                SqlCommand cmd = new SqlCommand("sp_IPPLOAssign");
                cmd.Connection = myConnection;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@AcadYear", lb_AcadYear.Text);
                cmd.Parameters.AddWithValue("@AcadPeriod", lb_AcadPeriod.Text);
                cmd.Parameters.AddWithValue("@IPPProjID", lb_ProjID.Text);
                cmd.Parameters.AddWithValue("@ProjSubID", "0");
                cmd.Parameters.AddWithValue("@LOLoginID", ddl_LO.SelectedValue);
                cmd.Parameters.AddWithValue("@LoginID", Session["UserID"].ToString());
                cmd.Parameters.AddWithValue("@Adminno", studadmin);
                myConnection.Open();
                cmd.ExecuteNonQuery();
                lb_Msg.Text = "Update Success.";
                lb_error.Text = "";

            }
            catch (Exception ex)
            {
                share.WriteLog(ex.Message, "LOAssign.aspx", "Button1_Click()", "Error in updating LO.");
                lb_error.Text = "Update failed.";
                lb_Msg.Text = "";
            }
            finally
            {
                if (myConnection.State == ConnectionState.Open)
                    myConnection.Close();
            }
        g++; //loop the subsequent studadmin and store into database
    }

    refresh_gvCompany();
    refresh_gvCurrent();  //refresh gridview
}

调试foreach语句时IndexOutOfBoundsException

您确认参数名称是否正确吗?也许@Adminno应该是@Adminno吗?

stud中的每个项都是大小为1的String数组。

然而,在每次迭代中,您都会增加数组的索引:

String studadmin = admin[g];
// ...
g++; // loop the subsequent studadmin and store into database

因此,您实际上尝试访问stud[0][0]stud[1][1]。。。这里出现错误,因为stud[1]只包含一个项目。

您需要删除ig——它们都是无用的。

由于您的数组只包含一个项,所以实际上看起来您误解了List和数组的用法。可能,您想要List<string>而不是List<string[]>:

protected void Button1_Click(object sender, EventArgs e)
{    
    List<string> stud = new List<string>();         
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox check = (CheckBox)row.FindControl("CheckBox1");
        if (check.Checked)
        {
            stud.Add(row.Cells[1].Text);
        }    
    }
    foreach (string studadmin in stud)
    {
        try
        {
            myConnection = db.getConnection();
            SqlCommand cmd = new SqlCommand("sp_IPPLOAssign");
            cmd.Connection = myConnection;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@AcadYear", lb_AcadYear.Text);
            cmd.Parameters.AddWithValue("@AcadPeriod", lb_AcadPeriod.Text);
            cmd.Parameters.AddWithValue("@IPPProjID", lb_ProjID.Text);
            cmd.Parameters.AddWithValue("@ProjSubID", "0");
            cmd.Parameters.AddWithValue("@LOLoginID", ddl_LO.SelectedValue);
            cmd.Parameters.AddWithValue("@LoginID", Session["UserID"].ToString());
            cmd.Parameters.AddWithValue("@Adminno", studadmin);
            myConnection.Open();
            cmd.ExecuteNonQuery();
            lb_Msg.Text = "Update Success.";
            lb_error.Text = "";       
        }
        catch (Exception ex)
        {
            share.WriteLog(ex.Message, "LOAssign.aspx", "Button1_Click()", "Error in updating LO.");
            lb_error.Text = "Update failed.";
            lb_Msg.Text = "";
        }
        finally
        {
            if (myConnection.State == ConnectionState.Open)
                myConnection.Close();
            }
    }
    refresh_gvCompany();
    refresh_gvCurrent(); 
}
相关文章:
  • 没有找到相关文章