ExecuteReader: Connection property has not been initialized

  • Thread starter Thread starter MattB
  • Start date Start date
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
 
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
 
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
 
Back
Top