I
Ian Burton
Hi,
I have a problem with the following:
Using vb.net I add a record to a Microsoft access 2000 table, and then I
immediately search for the record just added in order to get its primary
key. It can't find the primary key when I run the code normally, but if I
step through it, it works.
Any ideas? It is not a threaded application. Some of the code for the two
functions I am playing with is below.
Thanks in advance,
Ian Burton.
Public Function add(ByRef Organisation As OrganisationInfo) As Boolean
'returns the ID field into the company object
Dim oRequest As New dataRequest
Dim oParam As New dataRequest.Parameter
Dim oFactory As dataAbstractFactory
Dim result As Integer = 0
' Select the appropriate Concrete Factory to match your connection string
'oFactory = New dataSQLFactory
oFactory = New dataOleDbFactory
With oRequest
..Command = "INSERT INTO Organisation
(Organisation,Address,Postcode,Tel,Fax,DeletePassword) VALUES (" _
& "'" & Organisation.Organisation & "','" & Organisation.address & "','" _
& Organisation.postcode & "','" & Organisation.Tel & "','" &
Organisation.fax & "','" & Organisation.DeletePassword & "');"
..CommandType = CommandType.Text
..Transactional = False
End With
Try
result = oFactory.executenonquery(oRequest)
Catch ex As Exception
Debug.WriteLine(ex.Message)
Exit Function
End Try
If result <> 0 Then
'success
'now get the ID field from the database and put into the company object
Dim OrganisationID As Integer
'if i step through the next line It works, if I let it run it doesn't work.
OrganisationID = GetID(Organisation.Organisation)
If OrganisationID <> 0 Then
Organisation.ID = OrganisationID
Else
'it failed to get the id from the table for the just added ID, so there is
an error
Debug.Write("Added Organisation, but can't get the ID")
End If
Return True
Else
'failure - no rows affected
Return False
End If
End Function
Public Function GetID(ByVal Organisationname As String) As Integer
'returns the company table ID corresponding to the company name
Dim oRequestID As New dataRequest
Dim oParam As New dataRequest.Parameter
Dim oFactory As dataAbstractFactory
Dim oDataReader As dataAbstractDataReader
Dim dr As IDataReader
Dim OrganisationID As Integer
' Select the appropriate Concrete Factory to match your connection string
'oFactory = New dataSQLFactory
oFactory = New dataOleDbFactory
'set the request object
With oRequestID
..Command = "Select Orgc FROM Organisation WHERE Organisation='" &
Organisationname & "';"
..CommandType = CommandType.Text
..Transactional = False
End With
'run the command
Try
oDataReader = oFactory.ExecuteDataReader(oRequestID)
Catch ex As Exception
Debug.WriteLine(ex.Message)
Exit Function
End Try
'get the data out of the returned datareader
dr = oDataReader.ReturnedDataReader
While dr.Read
OrganisationID = IIf(IsDBNull(dr("Orgc")), 0, dr("Orgc"))
End While
dr.Close()
Return OrganisationID
End Function
I have a problem with the following:
Using vb.net I add a record to a Microsoft access 2000 table, and then I
immediately search for the record just added in order to get its primary
key. It can't find the primary key when I run the code normally, but if I
step through it, it works.
Any ideas? It is not a threaded application. Some of the code for the two
functions I am playing with is below.
Thanks in advance,
Ian Burton.
Public Function add(ByRef Organisation As OrganisationInfo) As Boolean
'returns the ID field into the company object
Dim oRequest As New dataRequest
Dim oParam As New dataRequest.Parameter
Dim oFactory As dataAbstractFactory
Dim result As Integer = 0
' Select the appropriate Concrete Factory to match your connection string
'oFactory = New dataSQLFactory
oFactory = New dataOleDbFactory
With oRequest
..Command = "INSERT INTO Organisation
(Organisation,Address,Postcode,Tel,Fax,DeletePassword) VALUES (" _
& "'" & Organisation.Organisation & "','" & Organisation.address & "','" _
& Organisation.postcode & "','" & Organisation.Tel & "','" &
Organisation.fax & "','" & Organisation.DeletePassword & "');"
..CommandType = CommandType.Text
..Transactional = False
End With
Try
result = oFactory.executenonquery(oRequest)
Catch ex As Exception
Debug.WriteLine(ex.Message)
Exit Function
End Try
If result <> 0 Then
'success
'now get the ID field from the database and put into the company object
Dim OrganisationID As Integer
'if i step through the next line It works, if I let it run it doesn't work.
OrganisationID = GetID(Organisation.Organisation)
If OrganisationID <> 0 Then
Organisation.ID = OrganisationID
Else
'it failed to get the id from the table for the just added ID, so there is
an error
Debug.Write("Added Organisation, but can't get the ID")
End If
Return True
Else
'failure - no rows affected
Return False
End If
End Function
Public Function GetID(ByVal Organisationname As String) As Integer
'returns the company table ID corresponding to the company name
Dim oRequestID As New dataRequest
Dim oParam As New dataRequest.Parameter
Dim oFactory As dataAbstractFactory
Dim oDataReader As dataAbstractDataReader
Dim dr As IDataReader
Dim OrganisationID As Integer
' Select the appropriate Concrete Factory to match your connection string
'oFactory = New dataSQLFactory
oFactory = New dataOleDbFactory
'set the request object
With oRequestID
..Command = "Select Orgc FROM Organisation WHERE Organisation='" &
Organisationname & "';"
..CommandType = CommandType.Text
..Transactional = False
End With
'run the command
Try
oDataReader = oFactory.ExecuteDataReader(oRequestID)
Catch ex As Exception
Debug.WriteLine(ex.Message)
Exit Function
End Try
'get the data out of the returned datareader
dr = oDataReader.ReturnedDataReader
While dr.Read
OrganisationID = IIf(IsDBNull(dr("Orgc")), 0, dr("Orgc"))
End While
dr.Close()
Return OrganisationID
End Function