How to make delete button for GridView in asp.net using C# for mysql databse

I created aboutcodes data base having reportcard table as show below

Make Idnumber colunm auto increment




Now go to aspx page and create GridView as show below




For customize GraidView make AutoGenerateColumns to False and DataField name
 Must be one of column name of reportcard table and DataKeyNames name column must be present in GridView column otherwise you will get error

Here DataKeyNames is idnumber, you can set DataKeyNames Visible to false as I done


Now go to aspx.cs page and create a function called reportdisplay

Reportdisplay function for display reportcard table display

Click here to make connection between mysql and asp.net

       

    private void reportdisplay()
    {
        string MyConString = "SERVER=localhost;" +
                   "DATABASE=aboutcodes;" +
                   "UID=root;" +
                   "PASSWORD=;";


        using (MySqlConnection connection = new MySqlConnection(MyConString))
        {

            try
            {


                connection.Open();
                string cmdtText = "SELECT * FROM `reportcard`";
                MySqlCommand cmde = new MySqlCommand(cmdtText, connection);
                MySqlDataAdapter da = new MySqlDataAdapter(cmde);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                connection.Close();

            }//end of try
            catch (Exception)
            {
              
                Label1.Text= "Error In Connection";
                

            }//end of catch
        }//end of use


    }// end of reportdisplay function

       
 


Call reportdisplay function in Page_Load function

Now make delete_Click function for delete button

       

    protected void delete_Click(object sender, EventArgs e)
    {
        LinkButton lnkbtn = sender as LinkButton;
        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        int fileid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());

        string MyConString = "SERVER=localhost;" +
              "DATABASE=aboutcodes;" +
              "UID=root;" +
              "PASSWORD=;";
        using (MySqlConnection connection = new MySqlConnection(MyConString))
        {
            MySqlCommand command = connection.CreateCommand();
            try
            {
               
                connection.Open();

                string cmdText1 = "DELETE FROM `reportcard` WHERE `idnumber` = @id";
                MySqlCommand cmd1 = new MySqlCommand(cmdText1, connection);
                cmd1.Parameters.AddWithValue("@id", fileid);

                cmd1.ExecuteNonQuery();               
                connection.Close();

                reportdisplay();
            }
            catch (Exception)
            {
                Label1.Text= "Error In Connection";
            }
        }//use

    }


       
 

   
The table looks like below



Leave reply

Add your comments here

Back to Top