?
Solved

Invalid attempt to read when no data is present.

Posted on 2005-03-24
2
Medium Priority
?
329 Views
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
        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!
0
Comment
Question by:dbMe
2 Comments
 
LVL 12

Accepted Solution

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

Author Comment

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

Thanks!
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
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…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

621 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