Avatar of codefinger
codefinger
Flag 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
        Try
            SQLCeAdapter = New System.Data.SqlServerCe.SqlCeDataAdapter(strsql, _conobj)
            SQLCeAdapter.InsertCommand = sqlcomm
            For Each sparm In vars
                SQLCeAdapter.InsertCommand.Parameters.Add(sparm)
            Next
            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

        Finally
            SQLCeAdapter.Dispose()
        End Try

        Return retval
    End Function

Open in new window

Visual Basic.NETMicrosoft SQL ServerMySQL Server

Avatar of undefined
Last Comment
Inteqam

8/22/2022 - Mon
QPR

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
Shahan Ayyub

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.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
codefinger

ASKER
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?

QPR

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

ASKER
QPR:

If the inserts are being rolled back, won't max(fieldname) always return the same value and therefore tell me nothing useful?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
QPR

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
QPR

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
Inteqam

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
codefinger

ASKER
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

codefinger

ASKER
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)
QPR

Can u try running your SQL at the database level (management studio) and see if it works?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Inteqam

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)


SELECT @RETURN=SCOPE_IDENTITY

Transaction gets rolled back (by database?)
codefinger

ASKER
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
QPR

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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
codefinger

ASKER
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.

QPR

What dies the generated SQL statement look like?
ASKER CERTIFIED SOLUTION
codefinger

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Inteqam

dont use the copy always property.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Inteqam

>>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?