2011-04-23 807 views
4

我不知何故得到一個雙插入;每次我提交表單時,它都會向數據庫發送兩條記錄。我無法弄清楚發生了什麼事。這是我的代碼看起來像一個總體思路:MS Access中雙插入?

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

更新:

最後的小片我.aspx.vb文件:

'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 

如果我把我的myIns1.ExecuteNonQuery()之前或之前的斷點,沒有任何插入。如果我把它放在myIns1.ExecuteNonQuery()之後,它會插入一次。如果我將它放在「End Sub」(在myConn.Close()下面),它會插入兩次。

+1

這些問號到底是什麼問題?我假設'myConn'是以前聲明的數據庫連接 – 2011-04-23 23:31:24

+0

是的,對不起,我把它從我的文章中刪除了,只是將它添加進去了。問號,我認爲它是Microsoft Access的東西?我不知道,除了Access之外,我還沒有真正使用過任何東西,這就是我被教導的方式......我認爲正常的語法是@whatever ......但我不認爲這適用於此。 – Sara 2011-04-23 23:36:22

+0

問號是OleDb的參數佔位符。它們是序數,而不是名義上的。 – 2011-04-23 23:48:14

回答

4

請確保aspx:button聲明沒有連接到代碼在線頁面上的onclick事件以及頁面後面的代碼。

0

如果您獲取重複插入,則程序在數據庫上調用插入兩次,或者數據庫導致重複(儘管這是可疑的,因爲您沒有調用可能隱藏重複的存儲過程您)。

由於問題更可能通過在C#程序中插入引起被調用兩次在您插入行添加調試行:

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) 

然後在調試模式下運行程序(按F5在如果您使用的是Visual Studio) - 如果插入行運行兩次,那麼您需要計算出原因,並拿走其中一個調用。

+0

我在插入行後添加了一個斷點,並在調試模式下運行它,當我這樣做時,似乎只插入一次... – Sara 2011-04-24 01:34:30

+0

是否有可能使用類似代碼的另一個頁面(您可以搜索您的網站以查找tableGrandmaster) - 它可能不是由於它的聲音而導致問題的代碼。 – amelvin 2011-04-24 10:31:57

+0

請檢查事件btnSubmit_Click是否也被其他事件觸發。 – Maxymus 2011-04-26 11:13:07