Gridview崩溃了-没有任何改变

本文关键字:任何 改变 崩溃 Gridview | 更新日期: 2023-09-27 18:18:59

我在ASP中创建一个应用程序。,团队领导可以为特定项目编辑每小时的目标。我计划今天要向很多人展示这个项目,我调试了这个项目,来排练一下,练习一下我的演讲。

然而,问题出现了。我从Gridview中检索要插入数据库的值的代码被打破了。

我收到的错误信息是:

类型为'System '的异常。参数outofrangeexception '发生在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;

完整的Update语句(删除数据库连接字符串和表名)。

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();

Gridview崩溃了-没有任何改变

你可以重构这个:

 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;
读:

var list = new List<string>();
foreach(TableCell cell in row.Cells)
   list.Add((cell.Controls[0] as TextBox).Text);

分别检索值的方式会有一点变化。但是,由于您的变量名称似乎遵循严格的顺序,无论如何,我不认为这是通过索引获取列表元素的问题。

例如,myControl.Text = txtH0;现在是myControl.Text = list[0];,如何得到'0'取决于你,它通常是循环的一部分。

有些人可能会说这并没有回答问题,而是一个代码审查——但是如果确实也解决了问题——那么现在就没有例外了。

注意:要小心假设你的控件肯定在那里,并且肯定是TextBox

我从@JayMee的回答中得到了解决方案。

更新的新代码是:

        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.ToString();
        string txtModel = row.Cells[2].Text.ToString();
        string txtProcess = row.Cells[3].Text.ToString();
        var list = new List<string>();
        for(int cell = 4; cell < 28; cell++)
        {
            list.Add((row.Cells[cell].Controls[0] as TextBox).Text.ToString());
        }
        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 + "','" + list[0] + "','" + list[1] + "','" + list[2] + "','" + list[3] + "','" + list[4] + "','" + list[5] + "','" + list[6] + "','" + list[7] + "','" + list[8] + "','" + list[9] + "','" + list[10] + "','" + list[11] + "','" + list[12] + "','" + list[13] + "','" + list[14] + "','" + list[15] + "','" + list[16] + "','" + list[17] + "','" + list[18] + "','" + list[19] + "','" + list[20] + "','" + list[21] + "','" + list[22] + "','" + list[23] + "','" + System.DateTime.Now.ToString(format) + "','" + User.Identity.Name.ToString() + "')";
        SqlCommand com = new SqlCommand(commandString, con);
        com.ExecuteNonQuery();
        con.Close();
        BindGridView();