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:
Try connection = New KobleTilDB con = connection.KobleTilDB sp1 = "INSERT INTO Kontaktinfo (Fornavn, Etternavn, Adresse, Postnr, Tlf, Mob, Mail) VALUES('" & ansatt.fnavn & "', '" & ansatt.enavn & _ "', '" & ansatt.adr & "', '" & ansatt.pnr & "', '" & ansatt.tlf & "', '" & ansatt.mob & _ "', '" & ansatt.epost & "')" cmd = New OdbcCommand(sp1, con) cmd.ExecuteNonQuery() sp3 = "SELECT Navn_id FROM Kontaktinfo " & _ "WHERE Fornavn = '" & ansatt.fnavn & "'" & _ "AND Etternavn = '" & ansatt.enavn & "'" & _ "AND Adresse = '" & ansatt.adr & "'" & _ "AND Postnr = '" & ansatt.pnr & "'" & _ "AND Tlf = '" & ansatt.tlf & "'" & _ "AND Mob = '" & ansatt.mob & "'" & _ "AND Mail = '" & ansatt.epost & "' " cmd = New OdbcCommand(sp3, con) Dim navnid As Integer = 0 cmd.ExecuteNonQuery() reader = cmd.ExecuteReader If reader.Read Then If reader.IsDBNull(0) Then navnid = 0 Else navnid = reader.GetInt32(0) End If End If sp2 = "INSERT INTO Admin (Navn_id, Tittel) VALUES('" & navnid & "', '" & ansatt.stilling & "')" cmd = New OdbcCommand(sp2, con) cmd.ExecuteNonQuery() Catch oex As OdbcException Throw New Exception("Feil i databasen" & oex.Message.ToString) Catch ex As Exception Throw New Exception("Feil i koden" & ex.Message.ToString) 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?![]()




Reply With Quote