Nor Azlina Mahmud

Nor Azlina Mahmud

  • NA
  • 23
  • 9.2k

Error when delete all gridview rows

Apr 15 2013 10:29 PM
I get attached error when I delete all gridview rows. how do i solve that?.. follows are my sample code..




protected void BindGridData1()
{
SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = new SqlCommand("Select * from FormTypeHeader ORDER BY fh_order", connString);

DataSet ds = new DataSet();
da.Fill(ds);
GridView3.DataBind();
GridViewRow FirstRow = GridView3.Rows[0];
ImageButton ImageUp = (ImageButton)FirstRow.FindControl("ImageUP");
ImageUp.Enabled = false;

GridViewRow LastRow = GridView3.Rows[GridView3.Rows.Count - 1];
ImageButton ImageDown = (ImageButton)LastRow.FindControl("ImageDOWN");
ImageDown.Enabled = false;
}

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = 0;
GridViewRow gvrow;
GridViewRow previousRow;

if (e.CommandName == "Up")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = GridView3.Rows[index];
previousRow = GridView3.Rows[index - 1];
int headerorder =
Convert.ToInt32(GridView3.DataKeys[gvrow.RowIndex].Values[0].ToString());
int fh_id = Convert.ToInt32(gvrow.Cells[1].Text);
int previousId = Convert.ToInt32(previousRow.Cells[1].Text);
connString.Open();
da.UpdateCommand = new SqlCommand("update form_headerA set fh_order='" +
(headerorder - 1) + "' where fh_id='" + fh_id + "'; update form_headerA set
fh_order='" + (headerorder) + "' where fh_id='" + previousId + "'",
connString);

da.UpdateCommand.ExecuteNonQuery();
connString.Close();
}

if (e.CommandName == "Down")
{
index = Convert.ToInt32(e.CommandArgument);
gvrow = GridView3.Rows[index];
previousRow = GridView3.Rows[index + 1];
int headerorder = Convert.ToInt32(GridView3.DataKeys[gvrow.RowIndex].Values[0].ToString());
int fh_id = Convert.ToInt32(gvrow.Cells[1].Text);
int previousId = Convert.ToInt32(previousRow.Cells[1].Text);
connString.Open();
da.UpdateCommand = new SqlCommand("update form_headerA set fh_order='" + (headerorder + 1) + "' where fh_id='" + fh_id + "'; update form_headerA set
fh_order='" + (headerorder) + "' where fh_id='" + previousId + "'",
connString);
da.UpdateCommand.ExecuteNonQuery();
connString.Close();
}


if (e.CommandName == "Exclude")
{
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
int fh_id =
Convert.ToInt32(GridView3.DataKeys[row.RowIndex].Values["fh_id"].ToString());
connString.Open();

da.DeleteCommand = new SqlCommand("DELETE FROM form_headerA WHERE fh_id ='" +
fh_id + "'", connString);
da.DeleteCommand.ExecuteNonQuery();
connString.Close();
}
BindGridData1();
BindGridData();
}

Answers (1)