Hi All,
I have decided to make the move over from classic ASP to dot net. I have written my first scraping script that reads from a database website addresses then loops through a list of keywords also on the database.
All works fine on my DEV maching but when i post it to the webserver i get. The error:
Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
I cant understand why this would happen? Can anyone provide some advise.
Thanks in advance
Tim
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="VB" runat="server">
Sub Page_Load(Sender as Object, E as EventArgs)
Dim Website, keywords As String
'// Connection string
Dim connString as String
connString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & _
"C:\Inetpub\www\WebsiteScrape\Scraping.mdb"
'// Database connection
Dim objConn As OledbConnection
objConn = New OledbConnection(connString)
objConn.Open() ' open connection
'// Get Websites
Dim strSQL, strKey As String
strSQL = "SELECT Website FROM Websites"
'// Create Website SQL Command
Dim objWebsites as OleDbCommand
objWebsites = New OledbCommand(strSQL, objConn)
'// Create Website Reader
Dim objReader As OledbDataReader
objReader = objWebsites.ExecuteReader(CommandBehavior.CloseConnection)
'// Loop through the Website addresses
Response.Write("<h2>Checking domain list</h2>")
While objReader.Read()
'// Get Keywords
strKey = "SELECT Keywords FROM Keywords"
'// Create Keywords SQL Command
Dim objKeywords as OleDbCommand
objKeywords = New OledbCommand(strKey, objConn)
'// Create Keyword Reader
Dim objReader2 As OledbDataReader
objReader2 = objKeywords.ExecuteReader(CommandBehavior.CloseConnection)
Response.Write("<b>Checking domain: </b><a href='" & objReader("Website") & "' target='_blank'>" & objReader("Website") & "</a><BR>")
'// Scrape the select websites
Website = objReader("Website")
Response.Write("<i>Scanning keywords:</i><BR>")
While objReader2.Read()
Response.Write(" » " & objReader2("Keywords") & " ")
keywords = objReader2("Keywords")
'// Loop keywords
Dim oRequest As WebRequest = WebRequest.Create(Website)
Dim oResponse As WebResponse = oRequest.GetResponse()
Dim oStream As Stream = oResponse.GetResponseStream()
Dim oStreamReader As New StreamReader(oStream, Encoding.UTF8)
Dim URL as string
Dim KeywordFinder As String
'Response.Write(oStreamReader.ReadToEnd())
URL=oStreamReader.ReadToEnd()
KeywordFinder = InStr(URL, keywords)
oResponse.Close()
oStreamReader.Close()
If KeywordFinder > 0 Then
Response.Write("YES (<i>First instance of '"& keywords &"' line: " & KeywordFinder & ")<BR>")
Else
Response.Write ("NO<BR>")
End If
End While
'// Clean variables
Website = ""
keywords = ""
End While
'// Close connections
objReader.Close()
End Sub
</script>
TDA