C# Gridview坏了-没有任何变化
我正在ASP.Net中创建一个应用程序,团队领导可以在其中编辑特定项目的每小时目标。我计划今天向多人展示这个项目,我调试了这个项目,以便进行一次演练并练习我的演讲 然而,出现了一个问题。我从Gridview中检索要插入数据库的值的代码已损坏 我收到的错误消息是: 中发生“System.ArgumentOutOfRangeException”类型的异常 System.Web.dll,但未在用户代码中处理 其他信息:指定的参数超出有效值的范围 我用于检索值的代码是:C# Gridview坏了-没有任何变化,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在ASP.Net中创建一个应用程序,团队领导可以在其中编辑特定项目的每小时目标。我计划今天向多人展示这个项目,我调试了这个项目,以便进行一次演练并练习我的演讲 然而,出现了一个问题。我从Gridview中检索要插入数据库的值的代码已损坏 我收到的错误消息是: 中发生“System.ArgumentOutOfRangeException”类型的异常 System.Web.dll,但未在用户代码中处理 其他信息:指定的参数超出有效值的范围 我用于检索值的代码是: string contract
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=新的SqlConnection(consting);
string contractName=gvShowData.DataKeys[e.RowIndex].Value.ToString();
GridViewRow行=(GridViewRow)gvShowData.Rows[e.RowIndex];
TextBox txtContractName=(TextBox)row.FindControl(“合同”);
//字符串txtCName=行。单元格[1]。文本;
//字符串txtModel=((文本框)行。单元格[2]。控件[0])。文本;
//字符串txtProcess=((文本框)行。单元格[3]。控件[0])。文本;
//字符串txtH0=((文本框)行。单元格[4]。控件[0])。文本;
//字符串txtH1=((文本框)行。单元格[5]。控件[0])。文本;
//字符串txtH2=((文本框)行。单元格[6]。控件[0])。文本;
//字符串txtH3=((文本框)行。单元格[7]。控件[0])。文本;
//字符串txtH4=((文本框)行。单元格[8]。控件[0])。文本;
//字符串txtH5=((文本框)行。单元格[9]。控件[0])。文本;
//字符串txtH6=((文本框)行。单元格[10]。控件[0])。文本;
//字符串txtH7=((文本框)行。单元格[11]。控件[0])。文本;
//字符串txtH8=((文本框)行。单元格[12]。控件[0])。文本;
//字符串txtH9=((文本框)行。单元格[13]。控件[0])。文本;
//字符串txtH10=((文本框)行。单元格[14]。控件[0])。文本;
//字符串txtH11=((文本框)行。单元格[15]。控件[0])。文本;
//字符串txtH12=((文本框)行。单元格[16]。控件[0])。文本;
//字符串txtH13=((文本框)行。单元格[17]。控件[0])。文本;
//字符串txtH14=((文本框)行。单元格[18]。控件[0])。文本;
//字符串txtH15=((文本框)行。单元格[19]。控件[0])。文本;
//字符串txtH16=((文本框)行。单元格[20]。控件[0])。文本;
//字符串txtH17=((文本框)row.Cells[21]。控件[0])。文本;
//字符串txtH18=((文本框)行。单元格[22]。控件[0])。文本;
//字符串txtH19=((文本框)行。单元格[23]。控件[0])。文本;
//字符串txtH20=((文本框)行。单元格[24]。控件[0])。文本;
//字符串txtH21=((文本框)行。单元格[25]。控件[0])。文本;
//字符串txtH22=((文本框)行。单元格[26]。控件[0])。文本;
//字符串txtH23=((文本框)行。单元格[27]。控件[0])。文本;
var list=新列表();
foreach(行中的变量单元格。单元格)
{
list.Add((单元格控件[0]作为文本框).Text);
}
gvShowData.EditIndex=-1;
con.Open();
DateTime=DateTime.Now;
字符串格式=“yyyy-MM-dd HH:MM:ss.fff”;
“插入到dms(合同、线、流程、线、线、流程、H0、H10、H11、H22、H0、H4、H55、H6、H7、H8、H9、H10、H11、H11、H11、H11、H11 1、线、线、线、流程、H0、H10、H10、H10、H11、H22、H3、H3、H13、H10、H10、H13、H10、H13、H10、H10、H13、H10、H10、H13、H10、H10、H10、H10、H10、H10、H13、H10、H10、H11、线、线、线、线、线、流程、流程、流程、H0、H0、H0、H10、H10、H10、H10、H10、H10、H10、H10、H10、H10、H10、H11、H10、H10、H11、H11、txtH6+“、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、“+System.DateTime.Now.ToString(格式)+”“+User.Identity.Name.ToString()+”)”;
SqlCommand com=新的SqlCommand(commandString,con);
com.ExecuteNonQuery();
con.Close();
BindGridView();
您可以重构:
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);
var list=newlist();
foreach(行中的表格单元格。单元格)
list.Add((单元格控件[0]作为文本框).Text);
然后单独检索这些值的方式会有一些变化。但由于变量名似乎遵循严格的顺序,我认为按索引获取列表元素不会有问题
例如,而不是myControl.Text=txtH0代码>现在是myControl.Text=list[0]代码>如何获得“0”取决于您,它通常是循环的一部分
有人可能会说,这并没有回答问题,而是一个代码检查——但它也解决了问题——现在也不会有例外
注意:请注意,假设您的控件肯定在那里,并且肯定是一个文本框
我通过@JayMee answer的一个rip得到了这个问题的解决方案
更新的新代码为:
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();
SqlConnection con=新的SqlConnection(consting);
string contractName=gvShowData.DataKeys[e.RowIndex].Value.ToString();
GridViewRow行=(GridViewRow)gvShowData.Rows[e.RowIndex];
TextBox txtContractName=(TextBox)row.FindControl(“合同”);
字符串txtCName=row.Cells[1].Text.ToString();
字符串txtModel=row.Cells[2].Text.ToString();
字符串txtProcess=row.Cells[3].Text.ToString();
var list=新列表();
用于(int单元格=4;单元格<28;单元格++)
{
list.Add((row.Cells[cell].Controls[0]作为文本框)。Text.ToString());
}
gvShowData.EditIndex=-1;
con.Open();
DateTime=DateTime.Now;
字符串格式=“yyyy-MM-dd HH:MM:ss.fff”;
插入到dms(合同、线路、流程、H0、H11、H22、H0、H4、H55、H6、H7、H8、H9、H10、H11、H11、H12、H12、H12、H12、H13、H13、H13、线、线、线、线、线、流程、过程、H0、H10、H10、H13、H10、H13、H10、H13、H10、H13、H10、H13、H13、H3、H13、H3、H10、H13、H10、H10、H10、H13、H10、H13、H10、H10、H10、H13、线、线、线、线、线、线、线、线、线、过程、过程、过程、H0、H0、H0、H0、H0、H10、H10、H10、H10、H10、H10、H10、H10、H10、H10、H10、"“+list[6]+”、“+list[7]+”、“+list[8]+”、“+list[9]+”、“+list[10]+”、“+list[11]+”、“+list[12]+”、“+list[13]+”、“+list[14]+”、“+list[15]+”、“+list[16]+”,
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();