嗨......,
我正在使用GridView来保存/更新/删除城市详细信息
当我尝试保存/更新/删除详细信息时,它将抛出一个错误“索引超出了数组的范围。”仅在具有已发布代码的服务器中
在我的本地机器上,它工作得很好。所以我将本地机器的连接字符串更改为服务器并在本地运行它。这也很好。
然后我检查了表格城市的数据库架构(列,键,约束,触发器,索引和统计)。一切都是一样的。
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;
请帮我解决这个问题....
提前谢谢...
此致