WFA_psychometric_chart.Form_handler.deleteHandlerToolStripMenuItem_Click C# (CSharp) Method

deleteHandlerToolStripMenuItem_Click() private method

private deleteHandlerToolStripMenuItem_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void deleteHandlerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //--This is the delete operation for the handler.....
            /*
            Steps: 1. delete the row  of the table using the id portion.
            2. Delete the corresponding tables related to the row.
            */
            if (indexSelectedForDeletion > -1)//Header is selected..
            {
                int selectedItemIndex = int.Parse(dataGridView1.Rows[indexSelectedForDeletion].Cells[0].Value.ToString());

                //we need to find the corresponding tables for deletion.

                int id = selectedItemIndex;

                string table1=null, table2=null;
                string databasePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string databaseFile = databasePath + @"\db_psychrometric_project.s3db";
                string connString = @"Data Source=" + databaseFile + ";Version=3;";

                using (SQLiteConnection connection = new SQLiteConnection(connString))
                {
                    connection.Open();
                    SQLiteDataReader reader = null;
                    string queryString = "SELECT *  from tbl_air_handler_details where id = @id_value";

                    SQLiteCommand command = new SQLiteCommand(queryString, connection);
                    command.Parameters.AddWithValue("@id_value", id);
                    //SqlDataAdapter dataAdapter = new SqlDataAdapter(queryString, connection.ConnectionString); //connection.ConnectionString is the connection string

                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        table1 = reader["node_table"].ToString();
                        table2 = reader["line_table"].ToString();
                    }

                }//Close of using

                //Deleting corresponding table
                deleteDatabaseTable(table1);
                deleteDatabaseTable(table2);

                //Deleting rows..

                using (SQLiteConnection connection = new SQLiteConnection(connString))
                {
                    connection.Open();
                  //  SQLiteDataReader reader = null;
                    string queryString = "delete from tbl_air_handler_details where id = @id_value";

                    SQLiteCommand command = new SQLiteCommand(queryString, connection);
                    command.Parameters.AddWithValue("@id_value", id);
                    //SqlDataAdapter dataAdapter = new SqlDataAdapter(queryString, connection.ConnectionString); //connection.ConnectionString is the connection string

                    command.ExecuteNonQuery();

                }//Close of using

            }//Close of if

            //--Refreshing the tables for all the datas..

            dataGridView1.Rows.Clear();
            DataGridView_Show_Data();
            dataGridView1.Rows.Add();
            //If there is no rows in datagridview...
              //if(dataGridView1.Rows.Count < 1) {

              //  dataGridView1.Rows.Add();//Adding new rows in datagridview.
              //  }else if (dataGridView1.Rows[dataGridView1.Rows.Count].Cells[0].Value != null)
              //  {
              //      dataGridView1.Rows.Add();//Adding new rows in datagridview.
              //  }
                    RefreshGraph();

            dataGridView2.Rows.Clear();
            MessageBox.Show("Handler deleted successfully !");
        }