Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DATAREADER Invalid attempt to Read when reader is closed.

Posted on 2004-10-06
12
Medium Priority
?
11,061 Views
Last Modified: 2011-10-03
Basically im having the error because the datareader is not finding any data to meet the query criteria. So it closes but returns nothing, how do i give back a message saying that there is no data? Code is Below


'This checks the date selected on the calendar and runs a query based on the selection
Sub Button1_Click(sender As Object, e As EventArgs)
    If month(formatdatetime(calendar1.SelectedDate,0)) = month(formatdatetime(system.DateTime.now,0)) or month(formatdatetime(calendar1.selectedDate,0)) > month(formatdatetime(system.DateTime.now,0)) then
    datagrid1.datasource = ""
    datagrid1.databind()
        Label1.text = "Please choose another date, at least a month back"
    Else
    datagrid1.datasource = Select_info(calendar1.selecteddate) ' DATA READER USED HERE
    datagrid1.databind()
        label1.text = "Select the campaign using the select button next to it to view detailed campaign information. You have selected campaign information from " & formatdatetime(Calendar1.SelectedDate,2) & " to " & formatdatetime(system.Datetime.now,2) & "."
    End if
End Sub


THIS IS THE FUNCTION used ABOVE :

    Function Select_info(ByVal datesel As Date) As System.Data.IDataReader
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=DATASOURCE
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

        Dim queryString As String = "SQL QUERY where [Start_Dte] >= @datesel) order by start_dte desc"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_datesel As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
        dbParam_datesel.ParameterName = "@datesel"
        dbParam_datesel.Value = datesel
        dbParam_datesel.DbType = System.Data.DbType.DateTime
        dbCommand.Parameters.Add(dbParam_datesel)

        dbConnection.Open
        Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
       
        Return dataReader
    End Function

Any help will be appreciated.

Thx
0
Comment
Question by:ridi786
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 8

Expert Comment

by:razo
ID: 12246020
Else
    datagrid1.datasource = Select_info(calendar1.selecteddate) ' DATA READER USED HERE
    datagrid1.databind()
if datagrid1.items.count<1 then
' no data found
else
        label1.text = "Select the campaign using the select button next to it to view detailed campaign information. You have selected campaign information from " & formatdatetime(Calendar1.SelectedDate,2) & " to " & formatdatetime(system.Datetime.now,2) & "."
    End if
0
 
LVL 1

Author Comment

by:ridi786
ID: 12246128
Hi still does not work.

Gives me the same error.

System.InvalidOperationException: Invalid attempt to Read when reader is closed.
Source Error:


Line 21:         Else
Line 22:         datagrid1.datasource = Select_info(calendar1.selecteddate)
Line 23:         datagrid1.databind() 'HIGHLIGHTS HERE
Line 24:         if datagrid1.items.count<1 then
Line 25:     ' no data found
 

I stand to be corrected but I think there needs to be some sort of statement in the function here

Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
       
        Return dataReader


0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12246134
Hi,

change this
 datagrid1.datasource = Select_info(calendar1.selecteddate) ' DATA READER USED HERE
    datagrid1.databind()

to

 Dim reader as DataReader = Select_info(calendar1.selecteddate)
 if reader.HasRows Then
    datagrid1.datasource = reader
    datagrid1.databind()
 else
 'show message
 end if


Regards,
B..M
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:ridi786
ID: 12246164
Type 'DataReader' is not defined.

Error thats coming up.
0
 
LVL 8

Expert Comment

by:razo
ID: 12246166
the datareader doesnt give an error when binded if it is empty
0
 
LVL 1

Author Comment

by:ridi786
ID: 12246181
Well it is giving me the error as below

System.InvalidOperationException: Invalid attempt to Read when reader is closed.
Source Error:


Line 21:         Else
Line 22:         datagrid1.datasource = Select_info(calendar1.selecteddate)
Line 23:         datagrid1.databind() 'HIGHLIGHTS HERE
Line 24:         if datagrid1.items.count<1 then
Line 25:     ' no data found

I think its not picking up data and closing, so its not returning anything, and thats creating the error.
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 2000 total points
ID: 12246199
use this

 Dim reader as System.Data.OleDb.OleDbDataReader = Select_info(calendar1.selecteddate)
 if reader.HasRows Then
    datagrid1.datasource = reader
    datagrid1.databind()
 else
 'show message
 end if


Regards,
B..M
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12246209
also you have to use

Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader()
instead of
Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

because the behaviour that you have specified close the reader


Regards,
B..M
0
 
LVL 1

Author Comment

by:ridi786
ID: 12246227
but then wont it still keep an open connection to the database until its closed?
0
 
LVL 1

Author Comment

by:ridi786
ID: 12246254
Ok its sorted, i didnt have to leave the reader open, as you said. What i did was just defined it with System.Data.OleDb.OleDbDataReader, and that accepted and worked. Thanks alot for the help :)
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12246259

if you want to close the connection, then use a dataset object
datareader object read a one-record at a time not all records
so if you close the connection, the reader will not be able to read from database
the dataset object get all data in memory and doesn't care if there is open connection
it dispose the object from the memory when you set it to null and the garbage collector goes through it
Regards,
B..M
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 12246317
Hi,
I think the problem is that the connection is getting closed when the function Select_info ends sotry by placing the line
 Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
globally i.e out of the select_info function. It should work

Regards,
Venki
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question