Best ms-access questions in April 2011

Double insert in MS Access?

4 votes

I'm somehow getting a double insert; every time I submit the form, it sends two records to the database. I can't figure out what's going on. This is a general idea of what my code looks like:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

'Data collection'

'Define Connection'
    Dim myConn As New OleDbConnection
    myConn.ConnectionString = adsGrandmaster.ConnectionString
    myConn.Open()

'Insert command'
    Dim myIns1 As New OleDbCommand("INSERT INTO tableGrandmaster (date_received, prefix, course_number, title, new, changed, inactivate, end_date, credits, description, hours_lecture, hours_lec_lab, hours_lab, hours_total, related_instruction, repeat, challengeable, in_catalog, in_printed_schedule, core_course, core_name, program_elective, program_name, prereqs, coreqs, recommended, green_course, code, dept_code, division_code, changing_depts, acti_code, grading, general_ed, writing, social_science, math, information_literacy, arts_letters, science_computer, speech_comm, cultural_literacy, date_curriculum_approval, date_state_sent, date_state_approval, date_created) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", myConn)

'Insert parameters'

'Execute command'
    myIns1.ExecuteNonQuery()

'Close connection'
    myConn.Close()

Update:

The last little piece of my .aspx.vb file:

'Execute command'
    myIns1.ExecuteNonQuery()

    Label1.Text = "Grandmaster submitted."

    'Close connection'
    myConn.Close()

End Sub

Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
    Response.Redirect("./index.htm")
End Sub
End Class

If I place my breakpoint at or before myIns1.ExecuteNonQuery(), nothing inserts. If I place it after myIns1.ExecuteNonQuery(), it inserts once. If I place it after "End Sub" (under myConn.Close()), it inserts twice.

Make sure that the aspx:button declaration is not wired up to the onclick event on the code-in-front page as well as the code behind page.