指定的参数已超出有效值的范围。

Skrr. 2019-05-07 12:33:42
请教下大佬帮忙看下,我这个改了很多次一直都是指定的参数已超出有效值的范围。。

点击编辑,更新列表出错。。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
///方法一:需改进
// string sqlstr = "update Teacher set teacher_id='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',teacher_name='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',teacher_sex='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',DepartmentName='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',Position='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',Telephone='"
//+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "' where teacher_id='"
//+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
// mycom = new SqlCommand(sqlstr,myconn);
// mycom.ExecuteNonQuery();
// GridView1.EditIndex = -1;
// tech_show();
///方法二:
//当前更新的行数
int index = e.RowIndex;
//(重点)获取格子里面,第index行,第0号单元格里面第0个控件(也就是TextBox)强转,获取值
string newsteach_id = ((TextBox)this.GridView1.Rows[index].Cells[0].Controls[0]).Text.ToString();
string newsteach_name = ((TextBox)this.GridView1.Rows[index].Cells[1].Controls[0]).Text.ToString();
string newsteach_sex = ((TextBox)this.GridView1.Rows[index].Cells[2].Controls[0]).Text.ToString();
string newsteach_xibu = ((TextBox)this.GridView1.Rows[index].Cells[3].Controls[0]).Text.ToString();
string newsteach_zhicheng = ((TextBox)this.GridView1.Rows[index].Cells[4].Controls[0]).Text.ToString();
string newsteach_tel = ((TextBox)this.GridView1.Rows[index].Cells[5].Controls[0]).Text.ToString();
//这里的id是绑定上去的主键
int id = Convert.ToInt32(this.GridView1.DataKeys[index].Value);
////////////////////////
GridView1.EditIndex = -1;
myconn.Open();
string str = "select * from Teacher";
mydata = new SqlDataAdapter(str, myconn);
myset = new DataSet();
mydata.Fill(myset, "teacher");
DataTable dt = myset.Tables["teacher"];
dt.PrimaryKey = new DataColumn[] { dt.Columns["teacher_id"] };
DataRow dr = dt.Rows.Find(id);
dr["teacher_id"] = newsteach_id;
dr["teacher_name"] = newsteach_name;
dr["teacher_sex"] = newsteach_sex;
dr["DepartmentName"] = newsteach_xibu;
dr["Position"] = newsteach_zhicheng;
dr["Telephone"] = newsteach_tel;
SqlCommandBuilder mycb = new SqlCommandBuilder(mydata);
mydata.Update(myset.Tables["teacher"]);
myconn.Close();
tech_show();

}



...全文
370 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kmbrkj 2019-05-13
  • 打赏
  • 举报
回复
.Controls用来读模板列列中的控件,如果不使用模版列,就没必要了
stherix 2019-05-07
  • 打赏
  • 举报
回复
Cells[x].Value.ToString就可以了 为什么要.Controls呢
  • 打赏
  • 举报
回复
我都是用自定义模板列的, 设置浏览和编辑 。
Re: 《文件和目录操作命令》 =================================== 本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到极致,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述! 《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》---------------------     

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧