In my code (see sample below) I have a connection to a DB and have opened a datareader on that connection. But within the datareader I need to execute SQL statements. When I try to do this I get the error message "there is already an open datareader associated with this connection which must be closed first".
Is there a way to execute a SQL command while looping through a datareader?
Dim ConnString As String = "dsn=brombergs"
' Set up query string
Dim CmdString As String = "select eventln.itemnum,webitemflag from eventln,item where item.itemnum=eventln.itemnum and eventln.eventmode='R' and (item.webitemflag<>'I' and item.webitemflag<>'Y')"
'Declare Connection and DataReader variables
Dim Conn2 As OdbcConnection
Dim Reader As OdbcDataReader
Dim itemnum As String
Dim storeemail As String
Try
'Open Connection
Conn2 = New OdbcConnection(ConnString)
Conn2.Open()
'Execute Query
Dim Cmd As New OdbcCommand(CmdString, Conn2)
Dim cmd2 As OdbcCommand
Reader = Cmd.ExecuteReader()
'Process The Result Set
While (Reader.Read())
itemnum = Reader("itemnum")
CmdString = "update item set webxmitflag='Y', webitemflag='Y' where itemnum='" & itemnum & "'"
cmd2 = New OdbcCommand(CmdString, Conn2)
'ERROR MESSAGE --> cmd2.ExecuteNonQuery()
End While
Finally
'Close Connection
Reader.Close()
Conn2.Close()
End Try
Is there a way to execute a SQL command while looping through a datareader?
Dim ConnString As String = "dsn=brombergs"
' Set up query string
Dim CmdString As String = "select eventln.itemnum,webitemflag from eventln,item where item.itemnum=eventln.itemnum and eventln.eventmode='R' and (item.webitemflag<>'I' and item.webitemflag<>'Y')"
'Declare Connection and DataReader variables
Dim Conn2 As OdbcConnection
Dim Reader As OdbcDataReader
Dim itemnum As String
Dim storeemail As String
Try
'Open Connection
Conn2 = New OdbcConnection(ConnString)
Conn2.Open()
'Execute Query
Dim Cmd As New OdbcCommand(CmdString, Conn2)
Dim cmd2 As OdbcCommand
Reader = Cmd.ExecuteReader()
'Process The Result Set
While (Reader.Read())
itemnum = Reader("itemnum")
CmdString = "update item set webxmitflag='Y', webitemflag='Y' where itemnum='" & itemnum & "'"
cmd2 = New OdbcCommand(CmdString, Conn2)
'ERROR MESSAGE --> cmd2.ExecuteNonQuery()
End While
Finally
'Close Connection
Reader.Close()
Conn2.Close()
End Try