ExecuteReader: Connection property has not been initialized

M

MattB

I'm trying to implement an example I found for displaying images stored in a
SQL database. The example code looks like this (in page_load):

Dim connstr As String = "Integrated Security=SSPI;Initial
Catalog=Northwind;Data Source=SERVER\netsdk"
Dim cnn As New SqlConnection(connstr)
Dim cmd As New SqlCommand
("select * from images where id=" & Request.QueryString("id"), cnn)
cnn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim bindata() As Byte = dr.GetValue(1)
Response.BinaryWrite(bindata)

I modified the connect string and query to fit my environment and ran the
page. I get that "ExecuteReader: Connection property has not been
initialized" error on this line: Dim dr As SqlDataReader =
cmd.ExecuteReader().

Can anyone tell me why or how to fix it? Thanks!

Matt
 
M

Mark Fitzpatrick

You created the connection objects and the command object, but you need to
set the connection property of the command object to equal your connection.
Otherwise, the command object has no knowledge about the connection.

cmd.Connection = cnn
cmd.Connection.Open()

that's the C# format but you should be able turn it into VB easily.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
M

MattB

Thanks!

Mark said:
You created the connection objects and the command object, but you
need to set the connection property of the command object to equal
your connection. Otherwise, the command object has no knowledge about
the connection.

cmd.Connection = cnn
cmd.Connection.Open()

that's the C# format but you should be able turn it into VB easily.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
Joined
Oct 4, 2007
Messages
1
Reaction score
0
Connection property has not been initialized

SelectCommand.Connection property has not been initialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top