Hi there,
I am trying to create an audit history for a project that I am working on. Each time I try to create an updated instance of a superseded record I get the following error at the highlighted line.
The error message returned is: "There is already an open DataReader associated with this Connection which must be closed first" at the first objCmdTemp.ExecuteNonQuery().
Does anyone know how I can fix this, or see what I am doing wrong?
Thanks in advance,
SonD
I am trying to create an audit history for a project that I am working on. Each time I try to create an updated instance of a superseded record I get the following error at the highlighted line.
Code:
Dim objCmdTemp As SqlCommand = New SqlCommand
objCmdTemp.Connection = objConn
Try
strSQL = "UPDATE tblProjects SET IsSuperseded=1, DateSuperseded=CONVERT(DateTime, '" & Now & "', 103) WHERE ProjectID=" & Request.QueryString("ProjectID")
objCmd.CommandText = strSQL
objCmd.ExecuteNonQuery()
strSQL = "INSERT INTO tblProjects " & strCols & " VALUES " & strVals
objCmd.CommandText = strSQL
objCmd.ExecuteNonQuery()
'get details of the identity of the last inserted record and pass back to the details page
strSQL = "SELECT Max(ProjectID) AS MaxPID FROM tblProjects"
objCmd.CommandText = strSQL
objDR = objCmd.ExecuteReader()
objDR.Read()
intMaxPID = objDR("MaxPID")
objDR.Close()
' we need to mark the existing studies as superseded and create new instances of these studies and point them to the new project id
strSQL = "SELECT * FROM tblStudies WHERE ProjectID=" & Request.QueryString("ProjectID") & " AND IsSuperseded=0 ORDER BY StudyID"
lblMessage.Text = ("strSQL: " & strSQL & "<br><br>")
objCmd.CommandText = strSQL
objDR = objCmd.ExecuteReader()
objDR.Read()
While objDR.Read
lblMessage.Text += "Studies found: "
lblMessage.Text += ("objDR(StudyID): " & objDR("StudyID") & "<br>")
strSQL = "INSERT INTO tblStudies "
strSQL += " (fieldnames) "
strSQL += " VALUES "
strSQL += " (" & objDR("fieldvalues") & ")"
lblMessage.Text += "strSQL : " & strSQL & "<BR><BR>"
objCmdTemp.CommandText = strSQL
objCmdTemp.ExecuteNonQuery()
'update the old study id as superseded
strSQL = "UPDATE tblStudies SET IsSuperseded=1, DateSuperseded=CONVERT(DateTime, '" & Now & "', 103)"
lblMessage.Text += "strSQL: " & strSQL & "<BR><BR>"
objCmdTemp.CommandText = strSQL
objCmdTemp.ExecuteNonQuery()
End While
objDR.Close()
Catch objExc
lblMessage.Text = "Unable to edit project - please try again."
lblMessage.Text += "<br>objExc source: " & objExc.Source
lblMessage.Text += "<br>objExc message: " & objExc.Message
lblMessage.Text += "<br>objExc stacktrace: " & objExc.StackTrace
End Try
The error message returned is: "There is already an open DataReader associated with this Connection which must be closed first" at the first objCmdTemp.ExecuteNonQuery().
Does anyone know how I can fix this, or see what I am doing wrong?
Thanks in advance,
SonD