# DataTable.Remove/Delete do not seem to be working ADO.NET Provider for Presto

**URL:** <https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556>\
**Category:** CData Drivers\
**Tags:** question, ado.net\
**Created:** [December 10, 2023, 11:08pm UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556 "2023-12-10T23:08:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocketmaniac](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rocketmaniac](https://community.cdata.com/u/rocketmaniac)\
**Post date:** [December 10, 2023, 11:08pm UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556/1 "2023-12-10T23:08:38Z")

</div>

```csharp
PrestoDataAdapter dataAdapter = new PrestoDataAdapter("SELECT _id FROM hive.transactional_schema.[TableName]", connection);dataAdapter.DeleteCommand = new PrestoCommand("DELETE FROM hive.transactional_schema.cached_reports where _id = @_id", connection);dataAdapter.DeleteCommand.Parameters.Add(new PrestoParameter("@_id", DbType.String, "_id"));DataTable table = new DataTable();dataAdapter.Fill(table);DataRow[] rowsToRemove = table.Select("_id = '657636888a673e46b8a4dce0'");foreach (var rowToDelete in rowsToRemove){ //table.Rows.Remove(rowToDelete); rowToDelete.Delete();}table.AcceptChanges();dataAdapter.Update(table);

```

I’ve tried both Remove and Delete and neither ends up executing a DELETE query on&nbsp;the Trino server, even&nbsp;though the DataTable shows the row as deleted.

---

<div class="post-metadata">

**Author:** ![Ankit\_Singh](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/ankit_singh/32/1614_2.png) [@Ankit\_Singh](https://community.cdata.com/u/Ankit_Singh)\
**Post date:** [December 11, 2023, 9:26am UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556/2 "2023-12-11T09:26:04Z")

</div>

Hi @rocketmaniac

The provided syntax appears to be a mix of C# code that interacts with a database using a `PrestoDataAdapter`, `PrestoCommand`, and `PrestoParameter` for executing SQL queries against a Presto database.

However, a few points need clarification or adjustment:

**1. Deletion Command Parameters:** Ensure that the parameter you’re using in the deletion query matches the parameter you’re adding to the **`PrestoCommand`.**

**2. Delete Rows** : The code snippet you provided seems to be trying to delete rows based on a specific condition **(`_id = ‘657636888a673e46b8a4dce0’`).** It’s important to ensure that the condition is correctly specified and matches your data.

**3. AcceptChanges() Method:** Depending on your use case, calling **`AcceptChanges()`** might not be necessary after removing rows. This method commits the changes made to the **`DataTable** `, marking all the remaining rows as unchanged.

Here’s an adjusted version of your code:

```csharp
PrestoDataAdapter dataAdapter = new PrestoDataAdapter("SELECT _id FROM hive.transactional_schema.[TableName]", connection);dataAdapter.DeleteCommand = new PrestoCommand("DELETE FROM hive.transactional_schema.cached_reports WHERE _id = @_id", connection);// Ensure the parameter matches the one used in the querydataAdapter.DeleteCommand.Parameters.Add(new PrestoParameter("@_id", DbType.String, "_id"));DataTable table = new DataTable();dataAdapter.Fill(table);DataRow[] rowsToRemove = table.Select("_id = '657636888a673e46b8a4dce0'");foreach (var rowToDelete in rowsToRemove){    // You can either remove the row from the table or mark it as deleted    rowToDelete.Delete();    // If you want to remove the row from the DataTable, uncomment the line below:    // table.Rows.Remove(rowToDelete);}// Call Update to reflect changes back to the databasedataAdapter.Update(table);

```

Make sure to replace **`hive.transactional_schema.[TableName]`** and **`hive.transactional_schema.cached_reports`** with the actual table names in your Presto database. Also, verify that the column name **`_id`** matches the one used in your database schema.

Remember, it’s crucial to handle exceptions, connection handling, and data validation/error checking appropriately in a production environment. Additionally, testing thoroughly with your specific database setup and schema is recommended.

Please give it a try and let me know if it works.

---

<div class="post-metadata">

**Author:** ![Luçjano\_Capo](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/lu%C3%A7jano_capo/32/1638_2.png) [@Luçjano\_Capo](https://community.cdata.com/u/Lu%C3%A7jano_Capo)\
**Post date:** [December 11, 2023, 3:31pm UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556/3 "2023-12-11T15:31:56Z")

</div>

Hi @rocketmaniac ,

You can use the DELETE Statement listed in our documentation to achieve your goal&nbsp;→&nbsp;[CData ADO.NET Provider for Presto - DELETE Statements](https://cdn.cdata.com/help/ORJ/ado/pg_delete.htm)

Your code would look something like this:

```csharp
using (PrestoConnection connection = new PrestoConnection(connectionString)) { PrestoCommand cmd = new PrestoCommand("DELETE FROM [TableName] WHERE _id = @_id", connection); cmd.Parameters.Add(new PrestoParameter("_id", "657636888a673e46b8a4dce0")); int rowsAffected = cmd.ExecuteNonQuery(); Console.Write("Rows affected: " + rowsAffected); }

```

Or if you want to keep the same format as the abovementioned, I was able to modify the code and got it to work using the following:

```csharp
            using (PrestoConnection connection = new PrestoConnection(connectionString)) { PrestoDataAdapter dataAdapter = new PrestoDataAdapter( "SELECT [_id] FROM [TableName]", connection); dataAdapter.DeleteCommand = new PrestoCommand( "DELETE FROM [TableName] WHERE _id = @_id", connection); // Add the parameters for the DeleteCommand. dataAdapter.DeleteCommand.Parameters.Add(new PrestoParameter( "@_id", "657636888a673e46b8a4dce0", DbType.Int32)); //dataAdapter.DeleteCommand.Parameters[0].SourceVersion = DataRowVersion.Original; dataAdapter.DeleteCommand.BeginExecuteNonQuery(); }

```

In the second section, I have placed&nbsp;the value of the **Id** parameter directly and then began the executing process, using the format → _new PrestoParameter( **VariableName** , **Value** , **DataType** )_

Please give this method a try and let me know if the issue is fixed.

---

<div class="post-metadata">

**Author:** ![rocketmaniac](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rocketmaniac](https://community.cdata.com/u/rocketmaniac)\
**Post date:** [December 12, 2023, 6:41pm UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556/4 "2023-12-12T18:41:13Z")

</div>

Thanks&nbsp; @Ankit_Singh &nbsp;the key change that made it work for me was excluding&nbsp;**AcceptChanges().**&nbsp; After excluding that, my original code worked, and the DELETE propagated to Trino and the row was deleted there.&nbsp; Apparently, once you&nbsp;**AcceptChanges()**&nbsp;then the dataAdapter has no record of the deleted row in “table”.&nbsp; I guess it makes sense, but it’s just not documented well anywhere that I found that if you call AcceptChanges, the delete will not propagate on dataAdapter.Update(table).

---

<div class="post-metadata">

**Author:** ![rocketmaniac](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@rocketmaniac](https://community.cdata.com/u/rocketmaniac)\
**Post date:** [December 12, 2023, 6:48pm UTC](https://community.cdata.com/t/datatable-remove-delete-do-not-seem-to-be-working-ado-net-provider-for-presto/556/5 "2023-12-12T18:48:55Z")

</div>

Thanks @Luçjano_Capo !&nbsp; As mentioned in my comment to @Ankit_Singh &nbsp;my original code worked once I removed the call to&nbsp;**AcceptChanges()**, so I didn’t actually test out your suggestions, but I’m sure they work as suggested.
