Caching results from a MySqlDataReader
Posted on 2007-10-09
I am wanting to cache the results of a MySqlDataReader which is then bound to a DropDownList. I have the following code, but get the error "Invalid attempt to Read when reader is closed." when calling "ddlSuburb.DataBind()". How do I fix this problem?
If Cache("SuburbList") Is Nothing Then
Dim ConnectionString As String = ConfigurationManager.ConnectionStrings("CMAConnection").ConnectionString
Dim CMAConnection As MySqlConnection = New MySqlConnection(ConnectionString)
Dim CMACommand As MySqlCommand = New MySqlCommand()
CMACommand.Connection = CMAConnection
CMACommand.CommandText = "ListSuburb"
CMACommand.CommandType = CommandType.StoredProcedure
Dim CMAReader As MySqlDataReader = CMACommand.ExecuteReader()
Cache("SuburbList") = CMAReader
Dim SuburbCache As MySqlDataReader = CType(Cache("SuburbList"), MySqlDataReader)
ddlSuburb.DataSource = SuburbCache