在DataGrid中更新数据的问题

迷失在太空 2006-10-31 02:37:10
我在一个Datagrid中设置了编辑,更新,取消按钮,点击编辑后进入编辑界面,更新一些字段的值后,点击更新按钮,总提示“Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index ”可是程序是我在家里测试好的,没有任何问题,当把相关DataGrid1_UpdateCommand代码拷贝到公司机器后就出现这个问题,请指教,我的更新按钮源代码如下:
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
String key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
String sqlUpdate = "update tblCustomer set name=@name,company=@company,address=@address,phone=@phone where id=@id";
sqlConnection1.Open();
SqlCommand sqlCommand = new SqlCommand(sqlUpdate,sqlConnection1);
sqlCommand.Parameters.Add("@id",key);
sqlCommand.Parameters.Add("@name",((TextBox)(e.Item.Cells[3].Controls[0])).Text);
sqlCommand.Parameters.Add("@company",((TextBox)(e.Item.Cells[5].Controls[0])).Text);
sqlCommand.Parameters.Add("@address",((TextBox)(e.Item.Cells[6].Controls[0])).Text);
sqlCommand.Parameters.Add("@phone",((TextBox)(e.Item.Cells[7].Controls[0])).Text);
sqlCommand.ExecuteNonQuery();
sqlConnection1.Close();
DataGrid1.EditItemIndex=-1;

String id,name;
id = TextBox1.Text;
name = TextBox2.Text;
sqlConnection1.Open();
sqlDataAdapter1.SelectCommand.Parameters["@id"].Value=id;
sqlDataAdapter1.SelectCommand.Parameters["@name"].Value=name;
sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
sqlDataAdapter1.Fill(dsTest1,"tblCustomer");
DataGrid1.DataBind();
}
...全文
116 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bejge1121 2006-10-31
  • 打赏
  • 举报
回复
单步调试看值是否传进去了呢~~???
GXY2005 2006-10-31
  • 打赏
  • 举报
回复
Index was out of range.
Index超范圍了,家里單位數據源不同造成的吧,調試下看看!
Knight94 2006-10-31
  • 打赏
  • 举报
回复
查看
e.Item.Cells[3]
是否有效

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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