Hesein Burg
Hesein Burg

Reputation: 329

Oracle Database, SQL Update statement will not work (OLEDB)

I set a numeric primary key, and an alphanumeric field that stores truck FINS, which is just a random mix of numbers and letters. I do not generate the fins, these fins will always be the same as it's the truck fleet identification number.

Here is the code view:

 storeTruckSplit = truckSplit[1];//Stores truck FIN


        //Update truck value
        try
        {
            conn.Open();
            OleDbCommand command;
            command = new OleDbCommand(
                "Update Trucks" +
                " SET Trucks.TruckInUse = ? WHERE TFIN = " + storeTruckSplit.ToString(), conn);
            command.Parameters.Add(new OleDbParameter("@use", "T"));
            command.ExecuteNonQuery();//Commit   
            conn.Close();
        }
        catch (OleDbException exception)
        {
            MessageBox.Show(exception.Message, "OleDb Exception");
        }

Here is the table view:

CREATE TABLE Trucks
(

TruckID number(9)  CONSTRAINT TRUCK_PK PRIMARY KEY,
TFIN char(9) NOT NULL,
TruckCategory varchar(80) NOT NULL,
TruckCodeName varchar(50) NOT NULL,
MaxWeight number(10) NOT NULL,
TruckSize number(10) NOT NULL,
TruckInUse varchar(1) NULL

);

There is also a sequence and trigger for before inserting on this table for the TRUCKID.

I am getting ORA-00904 and note this is showing C6977734D which is a truck fin that is used on the where clause of the update.

Exact Message:

"One or more errors occurred during processing of command. ORA-00904: "C6977734D": invalid indentifer.

Upvotes: 1

Views: 1016

Answers (1)

D Stanley
D Stanley

Reputation: 152556

Make the TFIN value a parameter as well:

    command = new OleDbCommand(
            "Update Trucks" +
            " SET Trucks.TruckInUse = ? WHERE TFIN = ?", conn);
        command.Parameters.Add(new OleDbParameter("@use", "T"));
        command.Parameters.Add(new OleDbParameter("@tfin", storeTruckSplit));
        command.ExecuteNonQuery();//Commit   

As it stands you are not putting quotes around the value you're filtering on, so the query is treating it as an identifier (field, variable, etc) rather than a constant value. Since you're already using a parameter for the "in use" value (which is not necessary since you're providing a constant value) the best fix is to use a parameter for the filter as well.

Upvotes: 1

Related Questions