Link to home
Start Free TrialLog in
Avatar of codefinger
codefingerFlag for United States of America

asked on

trying to insert values without success or error message:

Although the database connection is valid and  active,  the code attached does not provide an exception, nor does it succeed, even though affrows shows a value of 1, when I examine the database with database explorer  there are no additional rows.

What might I have failed to do?  

Thanks for anything that might steer me in the right direction...

Public Function InsertSQLwithVars(ByVal strsql As String, ByVal vars As ArrayList, ByRef lex As LastException) As Boolean
        Dim SQLCeAdapter As System.Data.SqlServerCe.SqlCeDataAdapter = Nothing
        Dim sparm As SqlServerCe.SqlCeParameter
        Dim retval As Boolean = True
        Dim sqlcomm As New SqlServerCe.SqlCeCommand
        sqlcomm.Connection = _conobj
        sqlcomm.CommandText = strsql
        Dim affrows As Integer = 0
            SQLCeAdapter = New System.Data.SqlServerCe.SqlCeDataAdapter(strsql, _conobj)
            SQLCeAdapter.InsertCommand = sqlcomm
            For Each sparm In vars
            affrows = SQLCeAdapter.InsertCommand.ExecuteNonQuery()

        Catch ex As Exception
            retval = False
            lex.ErrorMessage = ex.Message
            If Not ex.InnerException Is Nothing Then
                lex.ErrorMessage = lex.ErrorMessage & vbCrLf & ex.InnerException.Message
            End If

        End Try

        Return retval
    End Function

Open in new window

Avatar of QPR
Flag of New Zealand image

what is the commandtype? stored procedure?
What is the value of strsql?
Can you put strsql to a label to see what is actually being passed to the database
If all else fails, try removing the try/catch/finally/end try keywords to see if anything is thrown up
Avatar of Shahan Ayyub
Avatar of Inteqam

most probably your transaction is being rolled-back.

you can be sure by testing an AutoIncrement field, and see if the seed changes, if it does then, it has been added, then the addition has been rolled back.
Avatar of codefinger


integam, I think you may be on to something.  I didn't have nearly as much trouble with the database before I started trying to take advantage of the Auto-increment feature.  But I don't know how to check the AutoIncrement field in code.  Can you help me with that?

select max(fieldname)
do your inserts
select max(fieldname)

If the inserts are being rolled back, won't max(fieldname) always return the same value and therefore tell me nothing useful?
If the transaction was rolled back and the row(s) deleted, you can't reclaim the autonumber assigned.... at least that is how it works for a manual delete (I'm not 100% if a rolled back transaction has the same effect). Truncating a table resets autonumbers but not deletes.

I was answering the Q about how to check it based on intergams post about transactions rolling back but having had the autonumber incremented
A quick check suggests that rolled back transactions do lose their autonumbers.
The business rule for an identity field is purely to be unique, not necessarily sequential
i am positive that the autonumber does NOT reset in a roll back,

so, if you have set autonumbering true to a field, and added a record, it will take id 1, if you roll back, and then inset a new record, the new record will take the id 2.
Ok, I think either I am not clear (most likely), or you guys are kind of missing the point of my question...which is how to determine in code what the AutoIncrement number was BEFORE the transaction got rolled back.  Here:

Select(max(autoincrement field)) = 5
In code, do insert,
Transaction gets rolled back (by database?)
Select(max(autoincrement field)) = 5

SQL being passed to database is:

INSERT INTO COMMANDCODES (DeviceCode,VerbalCommand,ControlType,X10Command, ExternalProgram,RemoteCodesString,RemoteCodesBinary,RemoteCodesLength,RemoteCodesFrequency,Description) VALUES (@xDeviceCode,@xVerbalCommand,@xControlType,@xX10Command,@xExternalProgram,@xRemoteCodesString, @xRemoteCodesBinary,@xRemoteCodesLength,@xRemoteCodesFrequency,@xDescription)
Can u try running your SQL at the database level (management studio) and see if it works?
if it is AutoIncrement, you do not need to use the Max function

Instead use the @@SCOPE_IDENTITY constant

INSERT INTO COMMANDCODES (DeviceCode,VerbalCommand,ControlType,X10Command, ExternalProgram,RemoteCodesString,RemoteCodesBinary,RemoteCodesLength,RemoteCodesFrequency,Description) VALUES (@xDeviceCode,@xVerbalCommand,@xControlType,@xX10Command,@xExternalProgram,@xRemoteCodesString, @xRemoteCodesBinary,@xRemoteCodesLength,@xRemoteCodesFrequency,@xDescription)


Transaction gets rolled back (by database?)
In my case, the identity column DOES increment on every insert.

(It took me awhile to figure this out because SQLSERVER CE has some slightly different rules.  It does not support SCOPE_IDENTITY() or Parameter.Direction = Output.)

So it does appear my transaction is being made, then rolled back by the database.  I really wish the database would speak up and tell me WHY
Do you have a trigger on the table? Any constraints? Have you tried capturing the SQL string prior to execution to see if it looks as expected?
I would not know how to put a trigger on the database if I wanted to.  Violated constraints and bad SQL have always returned exceptions.

What dies the generated SQL statement look like?
Avatar of codefinger
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dont use the copy always property.
>>That just leaves me with one question  --- how are the updates I make in Debug mode supposed to get back to the "original" database file.  As far as I can tell, copy always only goes one direction.

this is another question, no?