Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert to SQLExpress table

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 22 '09
Hello,

Trying to just insert a simple record in DB and it fails. When I set debugger on, I see that execution jumps out by IntakeNummer to the Catch and of course, assigns value -1 to IntakeNummer. As if it was impossible to increment or even read de identity column! In my DB, IntakeID is my identity column (1,1).

Underneath the vb.net code that fails. Can anyone help me? What am I doing wrong?

---------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.   Public Function SetIntake(ByVal ProjectNummer As Integer, ByVal Status As String, ByVal DatumIntake As String, ByVal TelefonischAfspraak As String, _
  2.         ByVal Doelgroep As String, ByVal AantalDeelnemers As Integer, ByVal DataUitvoering As String, _
  3.         ByVal Tijden As String, ByVal OndersteuningBij As String, ByVal HandvattenVoor As String, _
  4.         ByVal LastigeSituaties As String, ByVal Opmerkingen As String, ByVal ActeurNodig As String, _
  5.         ByVal DatumAfgehandeld As String, ByVal ParamKlantID As Integer) As Integer
  6.  
  7.         Dim IntakeNummer As Integer = -1
  8.         Dim connectionString As String = getConnectionString()
  9.         Dim connection As New SqlConnection(connectionString)
  10.         Dim Query As New String("Insert Into Intake(" _
  11.             & "ProjectNummer, Status, DatumIntake, TelefonischAfspraak, " _
  12.             & "Doelgroep, AantalDeelnemers, DataUitvoering, Tijden, " _
  13.             & "OndersteuningBij, HandvattenVoor, LastigeSituaties, Opmerkingen, " _
  14.             & "ActeurNodig, DatumAfgehandeld, KlantID " _
  15.             & ") values ('" & ProjectNummer & "'," _
  16.             & "'" & Status & "'," _
  17.             & "'" & DatumIntake & "'," _
  18.             & "'" & TelefonischAfspraak & "'," _
  19.             & "'" & Doelgroep & "'," _
  20.             & "'" & AantalDeelnemers & "'," _
  21.             & "'" & DataUitvoering & "'," _
  22.             & "'" & Tijden & "'," _
  23.             & "'" & OndersteuningBij & "'," _
  24.             & "'" & HandvattenVoor & "'," _
  25.             & "'" & LastigeSituaties & "'," _
  26.             & "'" & Opmerkingen & "'," _
  27.             & "'" & ActeurNodig & "'," _
  28.             & "'" & DatumAfgehandeld & "'" _
  29.             & "'" & ParamKlantID & "'")
  30.  
  31.         Query = Query + New String("; Select @@IDENTITY")
  32.  
  33.  
  34.         Dim identity As Integer = -1
  35.         Dim Command As New SqlCommand
  36.  
  37.         Try
  38.             connection = New SqlConnection(connectionString)
  39.             Command.CommandType = CommandType.Text
  40.  
  41.             Command.CommandText = Query
  42.             Command.Connection = connection
  43.             Command.Connection.Open()
  44. 'JUMPS HERE TO CATCH AND ASSIGNS VALUE -1 TO INTAKENUMMER
  45.             IntakeNummer = Integer.Parse(Command.ExecuteScalar().ToString()) 
  46.             Command.ExecuteNonQuery()
  47.             Command.Connection.Close()
  48.         Catch ex As Exception
  49.             Dim err As String
  50.             err = ex.Message()
  51.             Return -1
  52.         End Try
  53.  
  54.         Return IntakeNummer
  55.     End Function

Reply

Tags
@@identity, insert, vb.net