Gridview Edit Delete and Update in ASP.NET
<p>I tried Implementing the code for Edit and Update button for gridview but it doesn't seems working for me. Add button working well but delete and update do not working. During runtime error the error is "An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code
Additional information: Unknown column 'p001' in 'where clause'"</p>

<p>Note: type of P_Id in the database is varchar(10), name varch(100), level varchar, value varchar</p>

<p>public partial class ManagePractice : System.Web.UI.Page
{</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindData();
    }
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
    TextBox txtID = (TextBox)GridView1.FooterRow.FindControl("txtID");
    TextBox txtSubject = (TextBox)GridView1.FooterRow.FindControl("txtSubject");
    RadioButtonList Level = (RadioButtonList)GridView1.FooterRow.FindControl("RadioButtonList2");
    RadioButtonList PType = (RadioButtonList)GridView1.FooterRow.FindControl("RadioButtonList1");
    AddPractice(txtID.Text.Trim(), txtSubject.Text.Trim(), Level.Text.Trim(), PType.Text.Trim());
    BindData();
}
private void AddPractice(string P_Id, string subject, string level, string type)
{
    string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";

    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "insert into practice(P_Id,name,level,value) values ('" + P_Id + "','" + subject + "','" + level + "','" + type + "')";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

    }
}
private void BindData()
{
    DataTable dt = new DataTable();
   string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        MySqlDataAdapter adp = new MySqlDataAdapter("select P_Id,level,name,value from practice", cn);
        adp.Fill(dt);

    }
    if (dt.Rows.Count &gt; 0)
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindData();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = (GridView1.DataKeys[e.RowIndex].Value.ToString ());
    DeletePractice(id);
    BindData();


}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    BindData();
}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  //  int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
    TextBox txtID = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtID");
    TextBox txtSubject = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSubject");
   // TextBox Level1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtlevel");
   // TextBox PType1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPType1");
     RadioButtonList Level = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList2");
     RadioButtonList PType = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("RadioButtonList1");

    UpdatePractice( txtID.Text , txtSubject.Text, Level.Text, PType.Text);
    GridView1.EditIndex = -1;
    BindData();

}
private void UpdatePractice( string P_Id, string name, string level, string value)
{
     string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "UPDATE practice SET P_Id='" + P_Id + "',name='" + name + "',level='" + level + "',value='" + value + " WHERE P_Id=" + P_Id + "";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}
private void DeletePractice(string id)
{
   string connStr = @"Data Source=localhost;Database=ahsschema;User Id=webuser;Password=webuser2014";
    using (MySqlConnection cn = new MySqlConnection(connStr))
    {
        string query = "DELETE FROM practice WHERE P_Id=" + id + "";
        MySqlCommand cmd = new MySqlCommand(query, cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}
</code></pre>