views:

35

answers:

2

Hey,

I created a button (Next) to navigate in a Table called CHAPTERS;

My problem is that the button work Two, and sometimes three times. After that I get [Not specified error].

This is my code :

Dim S As Integer = Integer.Parse(Request.QueryString("id"))
Dim RQ As String
Dim DR As OleDbDataReader
RQ = "SELECT ID_C FROM CHAPTRES"
DR = Connexion.lecture(RQ)
While DR.Read
    If DR.GetInt32(0) = S Then
            Exit While
        End If
    End While

    If DR.Read = True Then
        S = DR.GetInt32(0)
        Response.Redirect("Chapitre.aspx?id=" & S)
    Else   
        // End of records (stop reading)
    End If

Thanks.

UPDATES :

This the connecter and lecture functions in my Connexion.vb file :

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class Connexion

Public Shared Function conecter() As OleDbConnection
    Dim MyConnexion As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" & System.AppDomain.CurrentDomain.BaseDirectory & "/Learning.mdb")
    MyConnexion.Open()
    Return MyConnexion
End Function

Public Shared Function lecture(ByVal requete As String) As OleDbDataReader
    Dim Mycommand As OleDbCommand = conecter().CreateCommand()
    Mycommand.CommandText = requete
    Dim myReader As OleDbDataReader = Mycommand.ExecuteReader()
    Return myReader

End Function
+1  A: 

Your problem is probably that you're not closing/disposing your OleDbDataReader. The Response.Redirect call takes you off to another page without closing the open data reader.

Try modifying your last chunk of code to this:

If DR.Read = True Then 
    S = DR.GetInt32(0) 
    DR.Close()
    DR.Dispose()
    Response.Redirect("Chapitre.aspx?id=" & S) 
Else    
    // End of records (stop reading) 
End If 

Update: It would obviously help if you provided more information, like maybe exactly what line of code in this example is throwing the exception.

MusiGenesis
it's still the same problem
dotNET
@AZIRAR: you don't have to down-vote an answer just because it isn't correct. I generally reserve downvotes for grossly inaccurate answers that are liable to mislead others. In your case, you should be in the habit of closing data readers, regardless of whether this is the cause of your current problem or not.
MusiGenesis
even if I close the datareader I get always the same error
dotNET
Are you trying to read from an Access database (*.mdb) or an Excel file (*.xls)?
MusiGenesis
from an Access database
dotNET
It looks like this error can occur as a result of a connection being left open. How does your `Connexion` object work? Does it maintain an open connection?
MusiGenesis
See updates ...
dotNET
@MusiGenesis `this error occur as a result of a connection being left open` Thanks a lot.
dotNET
A: 

Not sure if you've already seen this article, but it may be of help.

Garett
I don't think that's the problem. Connecting to an MDB file creates the temporary LDB file immediately, so if it were a permissions problems, it wouldn't work 2 or 3 times and then stop working - it would never work at all.
MusiGenesis