|
-
Apr 28th, 2011, 09:46 AM
#1
Thread Starter
Junior Member
Problem with DataReader in loop
I've wrote a program designed to loop through a text file containing email addresses and unsubscribe said email addresses in our CRM system.
I'm having a problem with the following code...
Code:
Do While obj_filereader2.Peek() <> -1
strcuremail = obj_filereader2.ReadLine()
Dim dbqCustNo As String = "SQL CODE HERE - SELECT CUSTOMER NUMBER"
Dim exeCustNo As New SqlCommand()
Dim SQLdr As SqlDataReader
exeCustNo.Connection = ConnStr
exeCustNo.CommandText = dbqCustNo
SQLdr = exeCustNo.ExecuteReader
If SQLdr.HasRows Then
While SQLdr.Read()
strCustNo = Trim(SQLdr("customer"))
If strCustNo.Length > 0 Then
'If ConnStr.State = ConnectionState.Closed Then ConnStr.Open()
Dim updCustDefl As String = "SQL CODE HERE - UPDATE DEFAULTS"
Dim exeCustDefl As New SqlCommand(updCustDefl, ConnStr)
exeCustDefl.ExecuteNonQuery()
Dim updCustNote As String = "SQL CODE HERE - UPDATE NOTES"
Dim exeCustNote As New SqlCommand(updCustNote, ConnStr)
exeCustNote.ExecuteNonQuery()
strCustNo = ""
End If
strUpdateCount += 1
Call Delay(2)
End While
SQLdr.Close()
txtline = txtline & strcuremail & " - " & strUpdateCount & " records processed." & vbNewLine
txtResults.Text = txtline
txtResults.Select(txtResults.Text.Length - 1, 0)
txtResults.ScrollToCaret()
txtproccount += 1
strUpdateCount = 0
Else
txtline = txtline & strcuremail & " - Skipped (Email not found)" & vbNewLine
txtResults.Text = txtline
txtResults.Select(txtResults.Text.Length - 1, 0)
txtResults.ScrollToCaret()
txtproccount += 1
SQLdr.Close()
End If
tipRecordCount.Text = "Records Processed: " & txtproccount
Loop
The program is falling over when trying to execute the update commands during the datareader loop, with the fabled 'There is already an open DataReader associated with this Command...' error.
This program has evolved as I've found problems/better ways of working, so it might be a case of myself just not using the DataAdapter right - I'm guessing I should be using a function of the open DataReader rather than the ExecuteNonQuery command. Heck, I could be doing the whole process backwards.
I'm new to .Net and I'm adjusting to the various commands and how they're used.
All help appreciated...
Before anyone offers it as a suggestion, it's SQL 2000, so the MultipleActiveResultSets tag in the connection string a lot of people suggest for the problem won't work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|