Results 1 to 15 of 15

Thread: My queries executes twice :s

  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.

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Posts
    133

    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?

  3. #3
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    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.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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?

  5. #5

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

    Re: My queries executes twice :s

    Quote 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

  6. #6

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

    Re: My queries executes twice :s

    Quote 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

  7. #7

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

    Re: My queries executes twice :s

    Quote 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.

  8. #8
    Addicted Member
    Join Date
    Sep 2004
    Posts
    133

    Re: My queries executes twice :s

    I still say set a break point and step through the code to see what is happening.

  9. #9

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

    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?

  10. #10
    Addicted Member
    Join Date
    Sep 2004
    Posts
    133

    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.
    Code:
     cmd = 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)

  11. #11

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

    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:
    1. Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
    2.         Dim fornavn As String
    3.         Dim etternavn As String
    4.         Dim adresse As String
    5.         Dim postnr As String
    6.         Dim poststed As String
    7.         Dim tlf As Integer
    8.         Dim mob As Integer
    9.         Dim mail As String
    10.         Dim stilling As String
    11.         Dim kontroller As LeggTilAnsattKontroller
    12.         Dim ansatt As Ansatt
    13.  
    14.  
    15.         fornavn = txtNavn.Text
    16.         etternavn = txtEnavn.Text
    17.         adresse = txtAdresse.Text
    18.         postnr = txtPostnr.Text
    19.         poststed = txtPoststed.Text
    20.         tlf = Convert.ToInt32(txtTlf.Text)
    21.         mob = Convert.ToInt32(txtMob.Text)
    22.         mail = txtMail.Text
    23.         stilling = txtStilling.Text
    24.  
    25.         'Creating an object of the type ansatt
    26.         ansatt = New Ansatt(fornavn, etternavn, adresse, postnr, poststed, mail, tlf, mob, stilling)
    27.  
    28.         'Initializin the kontroller object which contains a reference to the DAO class which was the code I've already pasted
    29.         kontroller = New LeggTilAnsattKontroller
    30.        
    31.         'Calling on the method with the reference to the DAO class
    32.         kontroller.LeggTilAnsatt(ansatt)
    33.  
    34.        
    35.  
    36.     End Sub

  12. #12
    Addicted Member
    Join Date
    Sep 2004
    Posts
    133

    Re: My queries executes twice :s

    Is this an ASP.NET application?

    Do you have AutoEventWireUp set to True if it is?

  13. #13
    Member
    Join Date
    Feb 2006
    Posts
    45

    Re: My queries executes twice :s

    Try to remove the OnClick function in the aspx file

  14. #14

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

    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 =) )

  15. #15
    Member
    Join Date
    Feb 2006
    Posts
    45

    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
  •  



Click Here to Expand Forum to Full Width