I am completely baffled by this. It looks simple, and should have been simple but for some weird reason, it isn't.
It is a bit embarrassing but I have to get this up and running soon.
What I am trying to do basically is when users delete a record, grab the ID of the just deleted record, and insert it into our history table.
This works except that it *always* inserts duplicate records into the History table.
When I run the exact same code in SSMS query window, it works as expected - inserts one record at a time.
This leads me to believe there is some bug in my .net code but I can't see it.
Any help appreciated.
Below is all there is to it:
Thanks alot in advance
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim connStr As String = ConfigurationManager.ConnectionStrings("Connstr").ConnectionString
Dim cnn As SqlConnection
Dim cmd As SqlCommand
Dim sql As String = ""
' Indicate whether the delete operation succeeded by grabbing the ID of last deleted record.
Dim ID As Integer = CInt(e.Keys(0))
'So, who deleted a record?. We will find out by looking at the History table
sql += "Insert into DEDS ([employee_ID],[charity_code],[check_amt],[one_time],[bi_weekly],[cash],[donate_choice],[date_stamp],[amb_approved],[chcknum]) "
sql += " SELECT History.[employee_ID],[charity_code],[check_amt],[one_time],[bi_weekly],[cash],[donate_choice],[date_stamp],[amb_approved],[chcknum] from History where employee_id ='" & tmpEmplID & "' and year(date_stamp) = year(getdate()) and [ID] = " & ID & " "
cnn = New SqlConnection(connStr)
cmd = New SqlCommand(sql, cnn)