Question

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")`
Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top