Link to home
Start Free TrialLog in
Avatar of dbMe
dbMe

asked on

Invalid attempt to read when no data is present.

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
        dbCommand.Parameters.Add(dbParam_eventID)

        dbConnection.Open
        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

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of laotzi2000
laotzi2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dbMe
dbMe

ASKER

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

Thanks!