vb.net插入代码运行两次?

时间:2013-12-20 21:19:48

标签: asp.net code-behind sql-insert

我在vs2012(vb.net)中有一个aspx表单,其中创建了一个事件,并且在将记录插入表之后,它将新创建的ID传递到第二个表,其中它是外键。但是插入代码运行两次并在第二个表中创建两个条目。我试图将ID传递给函数并退出函数,但这也无效。有什么想法吗?

我不希望第二个表中的事件ID重复输入,所以我试图将事件ID插入第二个表,然后我可以更新第二个表。

    Protected Sub btnCreateIncident_Click(sender As Object, e As EventArgs) Handles btnCreateIncident.Click
    Try
        InciDS.InsertParameters("PatronID").DefaultValue = strPID
        InciDS.InsertParameters("PatronName").DefaultValue = PatName
        InciDS.InsertParameters("Inci_type").DefaultValue = ddInciCatList.SelectedItem.Text.Trim()
        InciDS.InsertParameters("Inci_date").DefaultValue = txtDate.Text.Trim()
        InciDS.InsertParameters("Inci_time").DefaultValue = ddEventHour.SelectedItem.Text.Trim() + ":" + ddEventMin.SelectedItem.Text.Trim()
        InciDS.InsertParameters("Active").DefaultValue = "True"
        InciDS.InsertParameters("LocDescription").DefaultValue = LocDesc.Text.Trim()
        InciDS.InsertParameters("Location").DefaultValue = ddLocation.SelectedItem.Text.Trim()
        InciDS.InsertParameters("Library").DefaultValue = ddBldg.SelectedItem.Text.Trim()
        InciDS.InsertParameters("Description").DefaultValue = txtInciDesc.Content
        InciDS.InsertParameters("Action_Taken").DefaultValue = txtActionTaken.Content
        InciDS.InsertParameters("Involved_patrons").DefaultValue = txtPatronDesc.Content
        InciDS.InsertParameters("FollowUp").DefaultValue = txtFollowUp.Content
        InciDS.InsertParameters("Created_By").DefaultValue = strCreatedBy.Text.Trim()
        InciDS.Insert()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    PassParameters = strPID
    PassParameters = PassParameters + "&value2=" + PatName
    PassParameters = PassParameters + "&value3=" + NewID

    Response.Redirect("~/IntermediatePage.aspx?value1=" & PassParameters, False)

End Sub

Protected Sub InciDS_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs) Handles InciDS.Inserted
    'get the newly id of the newly created incident
    NewID = e.Command.Parameters("@Inci_ID").Value.ToString
    PoliceDS.InsertParameters("IncidentID").DefaultValue = NewID
    PoliceDS.Insert()
    'Newid = Convert.ToInt16(e.Command.Parameters("@Inci_ID").Value)
End Sub

0 个答案:

没有答案