Double insert in MS Access?
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开发者_如何学Python.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.
If you're getting duplicate inserts either the program is calling an insert on the database twice, or the database is causing the duplicate (although this is doubtful as you are not calling a stored procedure that could be hiding the duplication from you).
As the problem is more likely to be caused by the insert in the c# program being called twice add a debug line on your insert line:
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)
Then run your program in debug mode (press F5 in Visual Studio if you are using it) - if the insert line is run twice then you need to work out why, and take away one of the calls.
精彩评论