Results 1 to 15 of 15

Thread: My queries executes twice :s

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Location
    Bergen, Norway
    Posts
    93

    My queries executes twice :s

    When I run my code (which doesn't have any loops) , the data is inserted into the database two times.

    So when I run one query, I get two lines in the database.

    This is the code where the queries are executed:

    VB Code:
    1. Try
    2.             connection = New KobleTilDB
    3.             con = connection.KobleTilDB
    4.            
    5.             sp1 = "INSERT INTO Kontaktinfo (Fornavn, Etternavn, Adresse, Postnr, Tlf, Mob, Mail) VALUES('" & ansatt.fnavn & "', '" & ansatt.enavn & _
    6.             "', '" & ansatt.adr & "', '" & ansatt.pnr & "', '" & ansatt.tlf & "', '" & ansatt.mob & _
    7.             "', '" & ansatt.epost & "')"
    8.  
    9.             cmd = New OdbcCommand(sp1, con)
    10.  
    11.             cmd.ExecuteNonQuery()
    12.  
    13.  
    14.             sp3 = "SELECT Navn_id FROM Kontaktinfo " & _
    15.             "WHERE Fornavn = '" & ansatt.fnavn & "'" & _
    16.             "AND Etternavn = '" & ansatt.enavn & "'" & _
    17.             "AND Adresse = '" & ansatt.adr & "'" & _
    18.             "AND Postnr = '" & ansatt.pnr & "'" & _
    19.             "AND Tlf = '" & ansatt.tlf & "'" & _
    20.             "AND Mob = '" & ansatt.mob & "'" & _
    21.             "AND Mail = '" & ansatt.epost & "' "
    22.  
    23.             cmd = New OdbcCommand(sp3, con)
    24.            
    25.             Dim navnid As Integer = 0
    26.  
    27.          
    28.             cmd.ExecuteNonQuery()
    29.  
    30.             reader = cmd.ExecuteReader
    31.  
    32.             If reader.Read Then
    33.                 If reader.IsDBNull(0) Then
    34.                     navnid = 0
    35.                 Else
    36.                     navnid = reader.GetInt32(0)
    37.                 End If
    38.             End If
    39.  
    40.             sp2 = "INSERT INTO Admin (Navn_id, Tittel) VALUES('" & navnid & "', '" & ansatt.stilling & "')"
    41.  
    42.             cmd = New OdbcCommand(sp2, con)
    43.             cmd.ExecuteNonQuery()
    44.  
    45.         Catch oex As OdbcException
    46.             Throw New Exception("Feil i databasen" & oex.Message.ToString)
    47.         Catch ex As Exception
    48.             Throw New Exception("Feil i koden" & ex.Message.ToString)
    49.  
    50.         End Try

    I have Three queries, and two of them are inserting values into two different tables.

    Why does the values get inserted two times in each table?
    Last edited by iste; Feb 16th, 2006 at 10:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width