Using VS2010 and SqlCE 3.5, I have the following code.
To retrieve the identity (autoincrement number), I created a query in CasesTableAdapter with the code SELECT @@IDENTITY AS TEMPVALUE and set the execution mode to scalar, but
returns zero even though the table adapter's update method did successfully add the record. No error is generated either.
I read that @@IDENTITY is supposed to work even if the connection to the database is closed and reopened. So I tried this.
The result I get is an Int32 but still zero. By the way, there's nothing strange about the identity itself. It starts at 1 and increments by 1.
Any ideas what I'm doing wrong? Thanks!
Me.Validate() Me.CasesBindingSource.EndEdit() Me.CasesTableAdapter.Update(Me.DbCases.cases) 'Now we need the identity of the record just added
To retrieve the identity (autoincrement number), I created a query in CasesTableAdapter with the code SELECT @@IDENTITY AS TEMPVALUE and set the execution mode to scalar, but
Dim LastRecID As Integer = Me.CasesTableAdapter.GetLastID()
returns zero even though the table adapter's update method did successfully add the record. No error is generated either.
I read that @@IDENTITY is supposed to work even if the connection to the database is closed and reopened. So I tried this.
Dim con As New SqlServerCe.SqlCeConnection(ConnectionString) Dim cmd As New SqlServerCe.SqlCeCommand Dim id As New Object cmd.CommandText = "SELECT @@IDENTITY AS TEMPVALUE" cmd.CommandType = CommandType.Text cmd.Connection = con cmd.Connection.Open() id = cmd.ExecuteScalar
The result I get is an Int32 but still zero. By the way, there's nothing strange about the identity itself. It starts at 1 and increments by 1.
Any ideas what I'm doing wrong? Thanks!
Last edited: