Com And Hosting

Recently I have come across with an issue when updating Oracle database from SQL data-reader. It does not update the table because of conversion failure and I was getting error message like this.

Here is the on click event code that I was trying to use.

Protected Sub btnPublish_Click(sender As Object, e As System.EventArgs)

        'Connect to the database and bring back the image contents & MIME type for the specified picture
        Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ApplicationServices").ConnectionString)

            Const ora As String = "SELECT * FROM  rtt_trails WHERE trail_id = @trail_id"
            Dim myCommand As New SqlCommand(ora, myConnection)

            myCommand.Parameters.AddWithValue("@trail_id", Request.QueryString("tid"))

            myConnection.Open()

            Dim myReader As SqlDataReader = myCommand.ExecuteReader

            If myReader.Read Then
                'Connect to the database and insert a new record into Products
                Using myOraConn As New OracleConnection(ConfigurationManager.ConnectionStrings("ratisDev").ConnectionString)

                    Try

                        Dim facCommand As New OracleCommand("ratis.ratis_lite.RL_UPDATE_TRL")
                        facCommand.Connection = myOraConn
                        'command type
                        facCommand.CommandType = CommandType.StoredProcedure

                        facCommand.Parameters.Add("P_TRAIL_ID", OracleType.Number).Value = Convert.ToInt32(myReader("TRAIL_ID"))
                        facCommand.Parameters.Add("P_FAC_ID", OracleType.Number).Value = Convert.ToInt32(myReader("FAC_ID"))
                        facCommand.Parameters.Add("P_FAC_ASSET_DESC", OracleType.VarChar).Value = myReader("FAC_ASSET_DESC")
                        facCommand.Parameters.Add("P_FAC_ASSET_CONDITION", OracleType.VarChar).Value = myReader("FAC_ASSET_CONDITION")
                        facCommand.Parameters.Add("P_FAC_ASSET_STATUS", OracleType.VarChar).Value = myReader("FAC_ASSET_STATUS")
                        facCommand.Parameters.Add("P_FAC_LOCATION", OracleType.VarChar).Value = myReader("FAC_LOCATION").ToString()
                        facCommand.Parameters.Add("P_FAC_REPLACEMENT_COST", OracleType.Number).Value = myReader("FAC_REPLACEMENT_COST")
                        facCommand.Parameters.Add("P_FAC_LONGITUDE_DEC", OracleType.Number).Value = myReader("FAC_LONGITUDE_DEC")
                        facCommand.Parameters.Add("P_FAC_LATITUDE_DEC", OracleType.Number).Value = myReader("FAC_LATITUDE_DEC")
                        facCommand.Parameters.Add("P_FAC_UNIVERSAL_ACCESS", OracleType.VarChar).Value = myReader("FAC_UNIVERSAL_ACCESS")
                        facCommand.Parameters.Add("P_FAC_COMMENTS", OracleType.VarChar).Value = myReader("FAC_COMMENTS")
                        facCommand.Parameters.Add("P_FAC_LAST_UPDATED", OracleType.DateTime).Value = ratislite.ToDateOrNull(myReader("FAC_LAST_UPDATED"))
                        facCommand.Parameters.Add("P_FAC_UPDATE_BY", OracleType.VarChar).Value = ratislite.ToStringOrNull(myReader("FAC_UPDATE_BY"))
                        facCommand.Parameters.Add("P_FAC_REVISED_COST", OracleType.Number).Value = myReader("FAC_REVISED_COST")
                        facCommand.Parameters.Add("P_FAC_REASON_COST_REVISED", OracleType.VarChar).Value = myReader("FAC_REASON_COST_REVISED")
                        facCommand.Parameters.Add("P_FACILITY_GROUP", OracleType.VarChar).Value = myReader("FACILITY_GROUP")

                        facCommand.Parameters.Add("P_TRAIL_LENGTH", OracleType.Number).Value = myReader("TRAIL_LENGTH")
                        facCommand.Parameters.Add("P_TRAIL_WIDTH", OracleType.Number).Value = myReader("TRAIL_WIDTH")
                        facCommand.Parameters.Add("P_TRAIL_TYPE", OracleType.VarChar).Value = myReader("TRAIL_TYPE")
                        facCommand.Parameters.Add("P_MARKERS", OracleType.VarChar).Value = myReader("MARKERS")
                        facCommand.Parameters.Add("P_CLASSIFICATION", OracleType.VarChar).Value = myReader("CLASSIFICATION")
                        facCommand.Parameters.Add("P_START_POINT", OracleType.VarChar).Value = myReader("START_POINT")
                        facCommand.Parameters.Add("P_END_POINT", OracleType.VarChar).Value = myReader("END_POINT")
                        facCommand.Parameters.Add("P_SURFACE", OracleType.VarChar).Value = myReader("SURFACE")
                        facCommand.Parameters.Add("P_DESIGN", OracleType.VarChar).Value = myReader("DESIGN")
                        facCommand.Parameters.Add("P_LATITUDE_START_POINT", OracleType.Number).Value = myReader("LATITUDE_START_POINT")
                        facCommand.Parameters.Add("P_LONGITUDE_START_POINT", OracleType.Number).Value = myReader("LONGITUDE_START_POINT")
                        facCommand.Parameters.Add("P_LATITUDE_END_POINT", OracleType.Number).Value = myReader("LATITUDE_END_POINT")
                        facCommand.Parameters.Add("P_LONGITUDE_END_POINT", OracleType.Number).Value = myReader("LONGITUDE_END_POINT")
                        facCommand.Parameters.Add("P_BICYCLE_CLASS", OracleType.VarChar).Value = myReader("BICYCLE_CLASS")
                        facCommand.Parameters.Add("P_TRAILHEAD", OracleType.VarChar).Value = myReader("TRAILHEAD")
                        facCommand.Parameters.Add("P_INTERP_SIGNAGE", OracleType.VarChar).Value = myReader("INTERP_SIGNAGE")
                        facCommand.Parameters.Add("P_OVERNIGHT", OracleType.VarChar).Value = myReader("OVERNIGHT")
                        facCommand.Parameters.Add("P_LENGTH_T1", OracleType.Number).Value = myReader("LENGTH_T1")
                        facCommand.Parameters.Add("P_LENGTH_T2", OracleType.Number).Value = myReader("LENGTH_T2")
                        facCommand.Parameters.Add("P_LENGTH_T3", OracleType.Number).Value = myReader("LENGTH_T3")
                        facCommand.Parameters.Add("P_TRK_NAME", OracleType.VarChar).Value = myReader("TRK_NAME")
                        facCommand.Parameters.Add("P_INTERNAL_PATHS", OracleType.VarChar).Value = myReader("INTERNAL_PATHS")

                        facCommand.Parameters.Add(New OracleParameter("rs", OracleType.Number)).Direction = ParameterDirection.Output

                        myOraConn.Open()

                        'execute insert command
                        facCommand.ExecuteNonQuery()

                        Dim count As Integer = 0
                        count = facCommand.Parameters("rs").Value

                        'log record if record is published successfully
                        If count > 0 Then

                            msg.InnerHtml = "This asset has been published to RecData"
                            msg.Attributes.Add("class", "sync")
                            ratislite.log_user("RTT_FACILITY_TRAILS", myReader("TRAIL_ID"), myReader("FAC_TYPE"), "Published: Updated trail and facility tables")
                            'change the sync flag to yes
                            dsUpdateSync.Update()
                        Else
                            msg.InnerHtml = "This asset cannot be published, either this record has been updated by another user or there must be some other issue/s."
                            msg.Attributes.Add("class", "error")
                        End If

                        'close connection
                        myOraConn.Close()

                    Catch ex As Exception
                        msg.InnerHtml = ex.ToString() ' "Sorry the record cannot be published, please make sure you are connected to DEC network." 'ex.ToString()
                        msg.Attributes.Add("class", "error")
                    Finally

                        'finally bind the data again
                        FormView1.DataBind()
                    End Try

                End Using

            End If
            myReader.Close()
            myConnection.Close()

        End Using
    End Sub

It is basically load the dataset in sqlDataReader and bind the variables in Oracle command parameters. It works fine when the last updated field (date) is not empty but it does not work when the field in null. I have tried to use custom function to convert the DBNull to null field but no luck. Here is the function I have tried but did not work –

Public Shared Function ToDateOrNull(ByVal value As String) As Object
        If Not IsDBNull(value) Then
            Return Date.Parse(value).Date

        End If

        Return OracleDateTime.Null.IsNull
        'DBNull.Value
    End Function

I am not sure what I did wrong there. Now I have changed the parameter line with very simple condition like below –

facCommand.Parameters.Add(“P_FAC_LAST_UPDATED”, OracleType.DateTime).Value = ratislite.ToDateOrNull(myReader(“FAC_LAST_UPDATED”))

to

facCommand.Parameters.Add("P_FAC_LAST_UPDATED", OracleType.DateTime).Value = If(myReader("FAC_LAST_UPDATED"), DBNull.Value)

It works fine for me and I can see the update pushed to Oracle Database.

Leave a Reply

Your email address will not be published. Required fields are marked *