views:

41

answers:

2

I am using the following code to update a table in oracle. It executes but the update does not commit. If I run the query with Oracle SQL Developer it works fine. What am I missing? SELECT statements work as expected.

`Dim BillOfLadingNumber As String = txtBillOfLadingNumber.Text.Trim

    Dim TrailerNumber As String = txtTrailerNumber.Text.Trim
    Dim CarrierCode As String = txtCarrierCode.Text.Trim
   Dim TransportationMethod As String = txtTransportationMethod.Text.Trim 
   Dim OracleCommand As New OracleCommand()
    With OracleCommand
        .Connection = OracleConnection
        .CommandType = CommandType.Text
        .CommandText = "UPDATE XXF_ASN_HEADERS SET BILL_OF_LADING_NUMBER ='" + BillOfLadingNumber + "',TRAILER_NUMBER ='" + TrailerNumber + "',CARRIER_CODE ='" + CarrierCode + "',TRANSPORTATION_METHOD ='" + TransportationMethod + "' WHERE HEADERID ='" + Request.QueryString("HeaderId") + "'"
    End With

    OracleConnection.Open()
    Dim result As Integer = OracleCommand.ExecuteNonQuery()
    OracleConnection.Close()

    If result = 1 Then Response.Redirect("default.aspx")`
A: 

The project I'm working on uses OracleCommand as well. The big difference is that all of our database calls go to stored procedures rather than dynamic sql that directly changes the database. Most likely, you'll need to wrap the update statement in a "BEGIN" and "END" block, and probably add a "COMMIT;" statement.

Look out though. At least one version of the .NET library for Oracle will fail if you put carriage returns and line feeds into CommandText. Just separate things with the semicolons.

John Fisher
The second option was to use stored procedures but I'm trying to get away from SP's to eliminate complexity in the maintenance of this application (will be handing it off to junior developers). It's not a huge application so doing some N-tier roll-out would be overkill, but you did help me resolve another issue I had open. thanks.
Geovani Martinez
A: 

Thank you all for pointing me out in the right direction. This is the final working code. The trick was using the OracleTransaction, the code below works like a charm. Parameter use are next. More information can be found here

            Using dbConnection As New OracleConnection(OracleConnectionString)

                'Open the connection
                dbConnection.Open()

                Dim dbCommand As OracleCommand = dbConnection.CreateCommand()
                Dim dbTransaction As OracleTransaction

                'Start a local transaction 
                dbTransaction = dbConnection.BeginTransaction(IsolationLevel.ReadCommitted)
                'Assign transaction object for a pending local transaction
                dbCommand.Transaction = dbTransaction

                Try
                    dbCommand.CommandType = CommandType.Text
                    dbCommand.CommandText = "UPDATE XXF_ASN_HEADERS SET BILL_OF_LADING_NUMBER ='" + BillOfLadingNumber + "', TRAILER_NUMBER ='" + TrailerNumber + "', CARRIER_CODE ='" + CarrierCode + "', TRANSPORTATION_METHOD ='" + TransportationMethod + "' WHERE HEADERID ='" + Request.QueryString("HeaderId") + "'"
                    dbCommand.ExecuteScalar()
                    dbTransaction.Commit()
                    Response.Redirect("default.aspx")
                Catch ex As OracleException
                    'Rollback the transaction
                    dbTransaction.Rollback()
                    'display error details
                    lblUpdateQuery.Text = dbCommand.CommandText
                    lblDebug.Text = ex.Message.ToString
                End Try

            End Using
Geovani Martinez