Click here to Skip to main content
15,171,576 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written code for deleting a row in a database table using Grid View. But the problem is the row is not getting deleted. I donot see any changes in the data in Grid View.Where am i wrong,help me with the correct code.Here is my code

C#
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
{
     string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
     deleterecordMethod(id);
     BindData();
}

private void deleterecordMethod(string id)
{
    string Query = "delete from SignUp where Employee_ID=@EmpID";
    try
    {
        SqlConObject.Open();
        SqlCommand cmd = new SqlCommand(Query, SqlConObject);
        cmd.Parameters.AddWithValue("@EmpID", id);
        cmd.ExecuteNonQuery();
    }
    catch(Exception e)
    {
        Response.Write("<script>alert('Exception Occurred')</script>"+e);
    }
    finally
    {

        SqlConObject.Close();
    }
}
Posted
Updated 24-Jul-12 1:44am
v4

As I see there is no problem in your code neither in Amit's answer, if you simply wanna see change in grid instantly then:
C#
private void deleterecordMethod(string id)
    {
        string Query = "delete from SignUp where Employee_ID=@EmpID";
        try
        {
            SqlConObject.Open();
            SqlCommand cmd = new SqlCommand(Query, SqlConObject);
            cmd.Parameters.AddWithValue("@EmpID", id);
            cmd.ExecuteNonQuery();
            //Call your FillGrid() method here to see effect in gridview.
        }
        catch(Exception e)
        {
            Response.Write("<script>alert('Exception Occurred')</script>"+e);
        }
        finally
        {

            SqlConObject.Close();
        }
    }
   
Hi,
Your query seems to be perfect. Just try using this:
C#
protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
         string id = gridRegistrationTableDetails.Rows[e.RowIndex].Cells[0].Text;
         deleterecordMethod(id);
         BindData();
    }
    private void deleterecordMethod(string id)
    {
        try
        {
            SqlConObject.Open();
            SqlCommand cmd = new SqlCommand("delete from SignUp where Employee_ID=@EmpID", SqlConObject);
            cmd.Parameters.AddWithValue("@EmpID", id);
            cmd.ExecuteNonQuery();
        }
        catch(Exception e)
        {
            Response.Write("<script>alert('Exception Occurred')</script>"+e);
        }
        finally
        {
            SqlConObject.Close();
        }
    }


Let me know if it helps you..



--Amit
   
thanks fr the help.it worked.. thanks fr ur feedbacks..
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900