Jono_2007
Jono_2007

Reputation: 63

can't get a delete command working

can someone explain what is wrong with this delete command, i am trying to delete a row from my table which contains some information about a flight and then replace it with a new one. The insertion works fine but the deletion won't work. any help would be great thanks

        OleDbCommand deleteCmd = new OleDbCommand();
        sqlStr = "DELETE FROM tblFlights WHERE Arrivals = '" + arrival + "'";

        connection.Open();
        // delete setup
        deleteCmd.Connection = connection;
        deleteCmd.CommandText = sqlStr;
        //activate the deletion
        dataAdapter.DeleteCommand = deleteCmd;

More info can be given if needed

Upvotes: 1

Views: 151

Answers (4)

Jamie Keeling
Jamie Keeling

Reputation: 9966

Your code is succeptable to SQL Injection attacks, take a look at my own question for ways to prevent such attacks and use secure queries - Method Optimisation - C#

Upvotes: 1

Adriano Carneiro
Adriano Carneiro

Reputation: 58615

You did not actually run the command. You're missing this:

deleteCmd.ExecuteNonQuery();

Also, you might want to look out for SQL Injection.

Upvotes: 2

TBohnen.jnr
TBohnen.jnr

Reputation: 5119

EDIT: as suggested above, you need to:

deleteCmd.ExecuteNonQuery();

for the command to actually be executed.

is Arrivals a string? if so then try and trim both values when it compares:

sqlStr = "DELETE FROM tblFlights WHERE LTRIM(RTRIM(Arrivals)) = LTRIM(RTRIM('" + arrival + "'))";        

If that does not work then do the below select and see what it returns:

SELECT * FROM tblFlights WHERE Arrivals = 'Your Arrival'

Upvotes: 1

Code Maverick
Code Maverick

Reputation: 20415

Please don't do this as it's prone to SQL injection

Upvotes: 1

Related Questions