Hi,
I am using GridView to Save/Update/Delete the City Details.
While I am trying to Save/Update/Delete the details, it will throws an error "Index was outside the bounds of the array." only in the server having the published code.
In my local machine it working perfectly. So I changed the connection string of the local machine to server and run it in the local. This also works fine.
Then I checked the DB architecture for the table city with (Columns, Keys, Constraints, Triggers, Indexes and Statistics ). All are same to same.
The code for GridView RowCommand is follows,
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;
Please help me to solve the error.......
Thanks in advance....
Jayasankar