|
-
Feb 16th, 2006, 10:30 AM
#1
Thread Starter
Lively Member
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:
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?
Last edited by iste; Feb 16th, 2006 at 10:42 AM.
-
Feb 16th, 2006, 11:46 AM
#2
Addicted Member
Re: My queries executes twice :s
Have you tried setting a break point and stepping through the code? I do not see in the code you have posted anything that would cause it to be inserted twice. What about the procedure that calls this one? Is there looping there?
-
Feb 16th, 2006, 11:52 AM
#3
Re: My queries executes twice :s
You do call :
cmd.ExecuteNonQuery()
reader = cmd.ExecuteReader
I have no idea if that would cause an error, I wouldnt think so, but you never know. You dont need to call the first execute as you do it when you call the reader.
-
Feb 16th, 2006, 11:54 AM
#4
Re: My queries executes twice :s
How are you verifying that it is being added twice? Are you physically looking at the database or viewing the data somewhere else? Maybe these multiple records were added previously?
-
Feb 16th, 2006, 12:25 PM
#5
Thread Starter
Lively Member
Re: My queries executes twice :s
 Originally Posted by schenz
What about the procedure that calls this one? Is there looping there?
I don't have any loops in my project only if sentences. :S
-
Feb 16th, 2006, 12:28 PM
#6
Thread Starter
Lively Member
Re: My queries executes twice :s
 Originally Posted by Grimfort
You do call :
cmd.ExecuteNonQuery()
reader = cmd.ExecuteReader
I have no idea if that would cause an error, I wouldnt think so, but you never know. You dont need to call the first execute as you do it when you call the reader.
but this code lies between the two insert sentences. If it is so, then why does the last insert also insert two lines? There are no reader = cmd.ExecuteReader after the last insert. :s
-
Feb 16th, 2006, 12:30 PM
#7
Thread Starter
Lively Member
Re: My queries executes twice :s
 Originally Posted by gigemboy
How are you verifying that it is being added twice? Are you physically looking at the database or viewing the data somewhere else? Maybe these multiple records were added previously?
I am physically looking at the database through phpMyAdmin at my webhotel. The tables were empty when I run the code, and then I checked and there were two lines with the same values.
-
Feb 16th, 2006, 12:35 PM
#8
Addicted Member
Re: My queries executes twice :s
I still say set a break point and step through the code to see what is happening.
-
Feb 16th, 2006, 01:17 PM
#9
Thread Starter
Lively Member
Re: My queries executes twice :s
I've tried with the break point and I've found that the last query is inserting the first query as well. I think each time I write "cmd = New OdbcCommand(sp1, con)" I add a query to the cmd ( even though I create a new one (I think)). So the last cmd.ExecuteNonQuery contains three queries, but only two of them leave a mark as one of them is a reading query.
How can I avoid this? There must be away to send three queries in the same function?
-
Feb 16th, 2006, 01:41 PM
#10
Addicted Member
Re: My queries executes twice :s
I can think of two ways to try off the top of my head (and without trying to do it)
1. After you run each command set the object to nothing.
2. Create 3 different command objects
Code:
Dim cmd1 as New OdbcCommand(sp1, con)
Dim cmd2 as New OdbcCommand(sp3, con)
Dim cmd3 as New OdbcCommand(sp2, con)
-
Feb 16th, 2006, 01:46 PM
#11
Thread Starter
Lively Member
Re: My queries executes twice :s
I have already tried this and it doesn't work.
anyway, I found that the whole code is runned twice. So the error can't lie in that code.
Here is the code where I call on the one I have already pasted:
VB Code:
Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
Dim fornavn As String
Dim etternavn As String
Dim adresse As String
Dim postnr As String
Dim poststed As String
Dim tlf As Integer
Dim mob As Integer
Dim mail As String
Dim stilling As String
Dim kontroller As LeggTilAnsattKontroller
Dim ansatt As Ansatt
fornavn = txtNavn.Text
etternavn = txtEnavn.Text
adresse = txtAdresse.Text
postnr = txtPostnr.Text
poststed = txtPoststed.Text
tlf = Convert.ToInt32(txtTlf.Text)
mob = Convert.ToInt32(txtMob.Text)
mail = txtMail.Text
stilling = txtStilling.Text
'Creating an object of the type ansatt
ansatt = New Ansatt(fornavn, etternavn, adresse, postnr, poststed, mail, tlf, mob, stilling)
'Initializin the kontroller object which contains a reference to the DAO class which was the code I've already pasted
kontroller = New LeggTilAnsattKontroller
'Calling on the method with the reference to the DAO class
kontroller.LeggTilAnsatt(ansatt)
End Sub
-
Feb 16th, 2006, 01:51 PM
#12
Addicted Member
Re: My queries executes twice :s
Is this an ASP.NET application?
Do you have AutoEventWireUp set to True if it is?
-
Feb 16th, 2006, 01:58 PM
#13
Member
Re: My queries executes twice :s
Try to remove the OnClick function in the aspx file
-
Feb 16th, 2006, 02:00 PM
#14
Thread Starter
Lively Member
Re: My queries executes twice :s
WOW! I never would have thought of that, but now it works =)
thanks Spanjis (and thanks to all you other folks to =) )
-
Feb 16th, 2006, 02:08 PM
#15
Member
Re: My queries executes twice :s
Lucky quess, I've had the same problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|