• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 515
  • Last Modified:

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

0
codefinger
Asked:
codefinger
  • 7
  • 7
  • 5
  • +1
1 Solution
 
QPRCommented:
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
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
0
 
InteqamCommented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
codefingerAuthor Commented:
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?

0
 
QPRCommented:
select max(fieldname)
go
do your inserts
go
select max(fieldname)
0
 
codefingerAuthor Commented:
QPR:

If the inserts are being rolled back, won't max(fieldname) always return the same value and therefore tell me nothing useful?
0
 
QPRCommented:
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
0
 
QPRCommented:
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
0
 
InteqamCommented:
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.
0
 
codefingerAuthor Commented:
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

0
 
codefingerAuthor Commented:
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)
0
 
QPRCommented:
Can u try running your SQL at the database level (management studio) and see if it works?
0
 
InteqamCommented:
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?)
0
 
codefingerAuthor Commented:
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
0
 
QPRCommented:
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?
0
 
codefingerAuthor Commented:
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.

0
 
QPRCommented:
What dies the generated SQL statement look like?
0
 
codefingerAuthor Commented:
Ok, figured this out, finally.    I had created one connection to the database in the location where I decided to keep it, however after adding the database to my project, when I run in debug mode, as I usually do, the entire database is copied to a different location.

So the database I am actually updating is NOT the one shown in the Database Explorer, it is the one in the bin/Debug directory.   So I would make my updates in my program then look for them in Database Explorer and be puzzled as to why they were not there.

I added a second connection to DataBase explorer to the database in the bin/Debug directory and changed the properties of the DataSource and the DataSet in the project to Copy Always.    Now when I make my updates in my program I can see them in Database Explorer under the second connection.

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.  

Thanks for any responses....
0
 
InteqamCommented:
dont use the copy always property.
0
 
InteqamCommented:
>>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?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 7
  • 7
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now