Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ExecuteReader: Connection property has not been initialized.

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I have the following code:
Code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load


        Dim conUsers As SqlConnection
        Dim SqlCommand As SqlCommand
        Dim strConnection As String

       
        strConnection = ConfigurationManager.ConnectionStrings("quickbabysitConnectionString").ConnectionString
        conUsers = New SqlConnection(strConnection)
        SqlCommand = New SqlCommand("SELECT [BookingFee] FROM [ParentsMembership] WHERE ([EmailAddress] = @EmailAddress)")
        SqlCommand.Parameters.AddWithValue("@EmailAddress", Session("UserID"))
        conUsers.Open()
        Dim dtrReader As SqlDataReader = SqlCommand.ExecuteReader()

        If dtrReader.HasRows Then
            'if you retrieve more than one row, you need the next line. If only one row, you can leave it out.
            'I will comment it out because you are returning only one row, especially with "TOP 1"
            'while dtrReader.Read()
            txtFee.Text = dtrReader("BookingFee")
            'end while
        Else
            Response.Write("No customer found for the supplied email.")
        End If

        dtrReader.Close()
        conUsers.Close()
    End Sub

I am getting the error ExecuteReader: Connection property has not been initialized.

Any ideas?

Thanks
 


SqlCommand = New SqlCommand("SELECT [BookingFee] FROM [ParentsMembership] WHERE ([EmailAddress] = @EmailAddress)"[red],strConnection[/red])



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Thanks. I now get the error:

Code:
Value of type 'String' cannot be converted to 'System.Data.SqlClient.SqlConnection'.

I added the line

Code:
SqlCommand.Connection = conUsers
after
Code:
SqlCommand.Parameters.AddWithValue("@EmailAddress", Session("UserID"))

but now I get the error message
Code:
System.Data.SqlClient.SqlException: The parameterized query '(@EmailAddress nvarchar(4000))SELECT [BookingFee] FROM [ParentsM' expects the parameter '@EmailAddress', which was not supplied.

on this line:
Code:
Dim dtrReader As SqlDataReader = SqlCommand.ExecuteReader()


 
I probably should say that I took your code out and added the line

Code:
SqlCommand.Connection = conUsers

as per my previous message. which works. but then I get the parametized error

My previous message didnt make sense
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top