【发布时间】:2015-10-23 19:37:12
【问题描述】:
我正在 ASP.Net 中创建一个应用程序,团队领导可以在其中编辑特定项目的每小时目标。我计划今天向多个人展示这个项目,我调试了这个项目,以进行演练并练习我的演讲。
但是,问题出现了。我从 Gridview 检索要插入数据库的值的代码已损坏。
我收到的错误信息是:
“System.ArgumentOutOfRangeException”类型的异常发生在 System.Web.dll 但未在用户代码中处理
附加信息:指定的参数超出了有效值的范围。
我用来检索值的代码是:
string contractName = gvShowData.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = (GridViewRow)gvShowData.Rows[e.RowIndex];
TextBox txtContractName = (TextBox)row.Cells[0].FindControl("Contract");
string txtCName = ((TextBox)row.Cells[1].Controls[0]).Text;
string txtModel = ((TextBox)row.Cells[2].Controls[0]).Text;
string txtProcess = ((TextBox)row.Cells[3].Controls[0]).Text;
string txtH0 = ((TextBox)row.Cells[4].Controls[0]).Text;
string txtH1 = ((TextBox)row.Cells[5].Controls[0]).Text;
string txtH2 = ((TextBox)row.Cells[6].Controls[0]).Text;
string txtH3 = ((TextBox)row.Cells[7].Controls[0]).Text;
string txtH4 = ((TextBox)row.Cells[8].Controls[0]).Text;
string txtH5 = ((TextBox)row.Cells[9].Controls[0]).Text;
string txtH6 = ((TextBox)row.Cells[10].Controls[0]).Text;
string txtH7 = ((TextBox)row.Cells[11].Controls[0]).Text;
string txtH8 = ((TextBox)row.Cells[12].Controls[0]).Text;
string txtH9 = ((TextBox)row.Cells[13].Controls[0]).Text;
string txtH10 = ((TextBox)row.Cells[14].Controls[0]).Text;
string txtH11 = ((TextBox)row.Cells[15].Controls[0]).Text;
string txtH12 = ((TextBox)row.Cells[16].Controls[0]).Text;
string txtH13 = ((TextBox)row.Cells[17].Controls[0]).Text;
string txtH14 = ((TextBox)row.Cells[18].Controls[0]).Text;
string txtH15 = ((TextBox)row.Cells[19].Controls[0]).Text;
string txtH16 = ((TextBox)row.Cells[20].Controls[0]).Text;
string txtH17 = ((TextBox)row.Cells[21].Controls[0]).Text;
string txtH18 = ((TextBox)row.Cells[22].Controls[0]).Text;
string txtH19 = ((TextBox)row.Cells[23].Controls[0]).Text;
string txtH20 = ((TextBox)row.Cells[24].Controls[0]).Text;
string txtH21 = ((TextBox)row.Cells[25].Controls[0]).Text;
string txtH22 = ((TextBox)row.Cells[26].Controls[0]).Text;
string txtH23 = ((TextBox)row.Cells[27].Controls[0]).Text;
整个更新语句(删除了数据库连接字符串和表名)。
SqlConnection con = new SqlConnection(constring);
string contractName = gvShowData.DataKeys[e.RowIndex].Value.ToString();
GridViewRow row = (GridViewRow)gvShowData.Rows[e.RowIndex];
TextBox txtContractName = (TextBox)row.FindControl("Contract");
//string txtCName = row.Cells[1].Text;
//string txtModel = ((TextBox)row.Cells[2].Controls[0]).Text;
//string txtProcess = ((TextBox)row.Cells[3].Controls[0]).Text;
//string txtH0 = ((TextBox)row.Cells[4].Controls[0]).Text;
//string txtH1 = ((TextBox)row.Cells[5].Controls[0]).Text;
//string txtH2 = ((TextBox)row.Cells[6].Controls[0]).Text;
//string txtH3 = ((TextBox)row.Cells[7].Controls[0]).Text;
//string txtH4 = ((TextBox)row.Cells[8].Controls[0]).Text;
//string txtH5 = ((TextBox)row.Cells[9].Controls[0]).Text;
//string txtH6 = ((TextBox)row.Cells[10].Controls[0]).Text;
//string txtH7 = ((TextBox)row.Cells[11].Controls[0]).Text;
//string txtH8 = ((TextBox)row.Cells[12].Controls[0]).Text;
//string txtH9 = ((TextBox)row.Cells[13].Controls[0]).Text;
//string txtH10 = ((TextBox)row.Cells[14].Controls[0]).Text;
//string txtH11 = ((TextBox)row.Cells[15].Controls[0]).Text;
//string txtH12 = ((TextBox)row.Cells[16].Controls[0]).Text;
//string txtH13 = ((TextBox)row.Cells[17].Controls[0]).Text;
//string txtH14 = ((TextBox)row.Cells[18].Controls[0]).Text;
//string txtH15 = ((TextBox)row.Cells[19].Controls[0]).Text;
//string txtH16 = ((TextBox)row.Cells[20].Controls[0]).Text;
//string txtH17 = ((TextBox)row.Cells[21].Controls[0]).Text;
//string txtH18 = ((TextBox)row.Cells[22].Controls[0]).Text;
//string txtH19 = ((TextBox)row.Cells[23].Controls[0]).Text;
//string txtH20 = ((TextBox)row.Cells[24].Controls[0]).Text;
//string txtH21 = ((TextBox)row.Cells[25].Controls[0]).Text;
//string txtH22 = ((TextBox)row.Cells[26].Controls[0]).Text;
//string txtH23 = ((TextBox)row.Cells[27].Controls[0]).Text;
var list = new List<string>();
foreach(var cell in row.Cells)
{
list.Add((cell.Controls[0] as TextBox).Text);
}
gvShowData.EditIndex = -1;
con.Open();
DateTime time = DateTime.Now;
string format = "yyyy-MM-dd HH:mm:ss.fff";
string commandString = "INSERT INTO dms (Contract, Line, Process, H0,H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,H12,H13,H14,H15,H16,H17,H18,H19,H20,H21,H22,H23,timestamp,username) VALUES('" + txtCName + "','" + txtModel + "','" + txtProcess + "','" + txtH0 + "','" + txtH1 + "','" + txtH2 + "','" + txtH3 + "','" + txtH4 + "','" + txtH5 + "','" + txtH6 + "','" + txtH7 + "','" + txtH8 + "','" + txtH9 + "','" + txtH10 + "','" + txtH11 + "','" + txtH12 + "','" + txtH13 + "','" + txtH14 + "','" + txtH15 + "','" + txtH16 + "','" + txtH17 + "','" + txtH18 + "','" + txtH19 + "','" + txtH20 + "','" + txtH21 + "','" + txtH22 + "','" + txtH23 + "','" + System.DateTime.Now.ToString(format) + "','" + User.Identity.Name.ToString() + "')";
SqlCommand com = new SqlCommand(commandString, con);
com.ExecuteNonQuery();
con.Close();
BindGridView();
【问题讨论】:
-
不要使用
Cell.FindControl,而是使用Row.FindControl。 Itemtemplate 中任何控件的NamingContainer是GridViewRow,而不是单元格。这使它更具可读性且不易出错。 -
正如错误提示
System.ArgumentOutOfRangeException可能在索引 27 处没有任何单元格,但您仍然试图在数据绑定事件中访问它。请遵循@TimSchmelter 方法。 -
我喜欢编程的众多原因之一是
an error has arisen, but nothing changed永远不会是真的。如果行为或执行路径发生变化,总会有某种变化。我的评论可能看起来没有建设性 - 但将其视为对可能引入错误的事情完全开放的建议。 -
我还建议(在您的演示之后)您在代码审查中得到这个并花一些时间考虑替代方法。这些变量声明和实例化的重复性正在呼唤一个循环。
-
@Paul:始终使用 sql 参数而不是字符串连接来防止 sql 注入。