mercredi 25 mars 2015

whats is going with code?Exception: Object reference not set to an instance of an object [duplicate]


This question already has an answer here:




I am working on gridview in asp.net c#. I have checkbox in first column of my gridview and with the checkbox i want to delete multiple row. But when I select rows and click delete button than chekcbox is not shown as checked.


protected void btnDelete_Click(object sender, EventArgs e) { int flag = 0; this.ConnectToDatabase();



for (int i = 0; i < myGrid.Rows.Count; i++)
{
CheckBox ck = (CheckBox)myGrid.Rows[i].FindControl("chkItem");

if (ck.Checked)
{
string storid = myGrid.Rows[i].Cells[1].Text;
MySqlCommand cmd = new MySqlCommand("Delete From MyFiles where ID =" + Int32.Parse(storid), conn);
// message += row.Cells["FileID"].Value.ToString();
cmd.ExecuteNonQuery();
flag = 1;
}
}

//foreach (GridViewRow row in myGrid.Rows)
//{
// if (row.RowType == DataControlRowType.DataRow)
// {

// if (((CheckBox)row.FindControl("chkItem")).Checked)
// {
// string storid = row.Cells[1].Text;
// MySqlCommand cmd = new MySqlCommand("Delete From MyFiles where ID =" + Int32.Parse(storid), conn);
// // message += row.Cells["FileID"].Value.ToString();
// cmd.ExecuteNonQuery();
// flag = 1;
// }
// }
//}
conn.Close();
if (flag == 1)
{
string mj = "Deleted Sucessfully";
ClientScript.RegisterStartupScript(this.GetType(), "yourMessage", "alert('" + mj + "');", true);
Response.Redirect("Main.aspx");

}
else
{
string msj = "Select Item to delete";
ClientScript.RegisterStartupScript(this.GetType(), "yourMessage", "alert('" + msj + "');", true);

}


}


...........................this grid view code.





Aucun commentaire:

Enregistrer un commentaire