1

在gridview保存/更新/删除上的数组错误范围之外的索引

嗨...,
我正在使用 GridView 来保存/更新/删除城市详细信息。
当我尝试保存/更新/删除详细信息时,它会抛出一个错误“索引超出了数组的范围。” 仅在具有已发布代码的服务器中。
在我的本地机器上,它运行良好。所以我把本地机器的连接字符串改成server,在本地运行。这也很好用。
然后我用(列、键、约束、触发器、索引和统计)检查了表城市的数据库架构。一切都一样。
GridView RowCommand 的代码如下,

City city = null;  
if (new[] { CommandName.DeleteRow.GetTextName(), CommandName.UpdateRow.GetName() }.Contains(e.CommandName))  
 city = City.GetCityById(int.Parse(e.CommandArgument.ToString()));  
if (e.CommandName == CommandName.StartInsert.GetTextName())  
{  
gvCities.FooterRow.Visible = true;  
return;  
}  
if (e.CommandName == CommandName.InsertFirst.GetTextName())  
{  
var button = (Button)e.CommandSource;  
if (button == null)  
  throw new Exception("Control not found");  
var txtName = button.NamingContainer.NamingContainer.FindControl("txtName") as TextBox;  
var ddlEmptyAddState = button.NamingContainer.NamingContainer.FindControl("ddlEmptyAddState") as DropDownList;  
if (txtName == null)  
  throw new Exception("Control not found");  
city = new City{ Name = txtName.Text.Trim(), StateId = Convert.ToInt32(ddlEmptyAddState.SelectedValue) };  
city.InsertCity();  
}  
else if (e.CommandName == CommandName.Insert.GetTextName())  
{  
var row = gvCities.FooterRow;  
DropDownList ddlAddState = (DropDownList)row.FindControl("ddlAddState");  
city = new City
{
Name = ((TextBox) row.FindControl("txtName")).Text.Trim(),
StateId = Convert.ToInt32(ddlAddState.SelectedValue),
IsDefault = ((CheckBox) row.FindControl("cbDefault")).Checked
};  
city.InsertCity();  
}  
else if (e.CommandName == CommandName.DeleteRow.GetTextName())  
{  
city.DeleteCity();  
}  
else if (e.CommandName == CommandName.UpdateRow.GetName())  
{  
var row = gvCities.Rows[gvCities.EditIndex];  
DropDownList ddlEditState = (DropDownList)row.FindControl("ddlEditState");  
if (row == null)  
  throw new Exception("Edit row not found");  
city.Name = ((TextBox) row.FindControl("txtName")).Text.Trim();  
city.StateId = Convert.ToInt32(ddlEditState.SelectedValue);  
city.IsDefault = ((CheckBox)row.FindControl("cbDefault")).Checked;  
city.UpdateCity();
}  
gvCities.DataBind();  
if (e.CommandName != CommandName.Edit.GetTextName() && gvCities.EditIndex >= 0)  
  gvCities.EditIndex = -1;  

请帮我解决这个问题....
提前谢谢...

问候

4

0 回答 0