C# 调试foreach语句时的IndexOutOfBoundsException
//我已经列出了下面的代码,请看一看,如果我能得到一些帮助,我将不胜感激。我的程序在字符串studadmin=admin[g]行抛出索引越界异常C# 调试foreach语句时的IndexOutOfBoundsException,c#,sql,list,foreach,indexoutofboundsexception,C#,Sql,List,Foreach,Indexoutofboundsexception,//我已经列出了下面的代码,请看一看,如果我能得到一些帮助,我将不胜感激。我的程序在字符串studadmin=admin[g]行抛出索引越界异常 protected void Button1_Click(object sender, EventArgs e) { //retrieve studentdetails using List<String[]; List<String[]> stud = new List<String[]>();
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
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
//使用列表检索studentdetails您是否确认您的参数名称完全正确?您是否确认您的参数名称完全正确?您是否确认您的参数名称完全正确?您的stud
中的每个项目都是大小为1的字符串数组。
但是,在每次迭代中,您都会增加数组的索引:
String studadmin = admin[g];
// ...
g++; // loop the subsequent studadmin and store into database
因此,您实际上尝试访问stud[0][0]
,stud[1][1]
。此处出现错误,因为stud[1]
仅包含一项
您需要删除i
和g
——它们都是无用的
由于您的数组只包含一个项,实际上您似乎误解了列表
和数组的用法。您可能想要一个列表
,而不是列表
:
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
列表螺柱=新列表();
foreach(GridView1.Rows中的GridViewRow行)
{
复选框check=(复选框)row.FindControl(“复选框1”);
如果(勾选,勾选)
{
stud.Add(row.Cells[1].Text);
}
}
foreach(螺柱中的字符串studadmin)
{
尝试
{
myConnection=db.getConnection();
SqlCommand cmd=新的SqlCommand(“sp_IPPLOAssign”);
cmd.Connection=myConnection;
cmd.CommandType=CommandType.storedProcess;
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=“更新成功。”;
lb_error.Text=“”;
}
捕获(例外情况除外)
{
WriteLog(例如,Message,“LOAssign.aspx”,“Button1\u Click()”,“更新LO时出错”);
lb_error.Text=“更新失败。”;
lb_Msg.Text=“”;
}
最后
{
if(myConnection.State==ConnectionState.Open)
myConnection.Close();
}
}
刷新公司();
刷新当前值();
}
您的螺柱中的每个项目都是大小为1的字符串数组
但是,在每次迭代中,您都会增加数组的索引:
String studadmin = admin[g];
// ...
g++; // loop the subsequent studadmin and store into database
因此,您实际上尝试访问stud[0][0]
,stud[1][1]
。此处出现错误,因为stud[1]
仅包含一项
您需要删除i
和g
——它们都是无用的
由于您的数组只包含一个项,实际上您似乎误解了列表
和数组的用法。您可能想要一个列表
,而不是列表
:
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
列表螺柱=新列表();
foreach(GridView1.Rows中的GridViewRow行)
{
复选框check=(复选框)row.FindControl(“复选框1”);
如果(勾选,勾选)
{
stud.Add(row.Cells[1].Text);
}
}
foreach(螺柱中的字符串studadmin)
{
尝试
{
myConnection=db.getConnection();
SqlCommand cmd=新的SqlCommand(“sp_IPPLOAssign”);
cmd.Connection=myConnection;
cmd.CommandType=CommandType.storedProcess;
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=“更新成功。”;
lb_error.Text=“”;
}
捕获(例外情况除外)
{
WriteLog(例如,Message,“LOAssign.aspx”,“Button1\u Click()”,“更新LO时出错”);
lb_error.Text=“更新失败。”;
lb_Msg.Text=“”;
}
最后
{
if(myConnection.State==ConnectionState.Open)
myConnection.Close();
}
}
刷新公司();
刷新当前值();
}