Invalid attempt to read when no data is present.

Posted on 2005-03-24
Medium Priority
Last Modified: 2008-02-01
How do I make this code work? I want to get the data from the method but I'm not sure how.  I'll up the points by 25 if I can get this figured out within the next hour.

Here's Line 768, which is causing the error below:
Dim amountOwed as Decimal = SelectEventCost(REG_CourseList.SelectedItem.Value).Item("Cost")

And the method that is being used:
    Function SelectEventCost(ByVal eventID As Integer) As System.Data.IDataReader
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "SELECT [Events].[Cost], [Events].[CoupleCost], [Events].[SiblingCost], [Events].["& _
        "FamilyCost], [Events].[MaxCost], [Events].[LateFee] FROM [Events] WHERE ([Events"& _
        "].[EventID] = @EventID)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_eventID As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_eventID.ParameterName = "@EventID"
        dbParam_eventID.Value = eventID
        dbParam_eventID.DbType = System.Data.DbType.Int32

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

        Return dataReader
    End Function

I get the following error:
System.InvalidOperationException: Invalid attempt to read when no data is present. at System.Data.SqlClient.SqlDataReader.PrepareRecord(Int32 i) at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) at System.Data.SqlClient.SqlDataReader.get_Item(String name) at ASP.Default_aspx.REG_btnPayLater_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\Whatever\Default.aspx:line 768

Question by:dbMe
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
LVL 12

Accepted Solution

laotzi2000 earned 300 total points
ID: 13625695
change this:
Dim amountOwed as Decimal = SelectEventCost(REG_CourseList.SelectedItem.Value).Item("Cost")
dim reader as sqldatareader
reader = SelectEventCost(REG_CourseList.SelectedItem.Value)
dim amountowed as decimal
if(reader.read()) then
  amountowed = reader.item("cost")
end if

Author Comment

ID: 13625951
Worked perfectly.  I was on the right track earlier but thought that I wasn't.


Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

762 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