ExecuteReader: Connection property not initialized

P

phil

Hi,

With the code below, i get the error:
ExecuteReader: Connection property has not been initialized.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: ExecuteReader:
Connection property has not been initialized.

Source Error:


Line 25: oConnection.Open()
Line 26: comd = New SqlCommand("select naam,type from pc")
Line 27: dtreader = comd.ExecuteReader
Line 28: x = 0
Line 29: If dtreader.HasRows Then


Source File: D:\Inetpub\wwwroot\aspnet\Begin\Chapter07\access2.aspx Line:
27
--------------------------------------------------------------
My code:
Dim oConnection As System.Data.OleDb.OleDbConnection
oConnection = New System.Data.OleDb.OleDbConnection()

Dim comd As SqlCommand
Dim dtreader As SqlDataReader
Dim x As Integer

Dim sConnectionString As String
sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
= d:\access\nemi.mdb"
oConnection.ConnectionString = sConnectionString

oConnection.Open()
comd = New SqlCommand("select name,type from pc")
dtreader = comd.ExecuteReader
x = 0
If dtreader.HasRows Then
While dtreader.Read
x = x + 1
Response.Write(dtreader.Item("naam"))
Response.Write("<br>")
End While
End If
Response.Write(x)
dtreader.Close()
oConnection.Close()


Thanks
Phil
 
J

joshuacoad

You need to assign the Connection oject to the Command object:

comd = New SqlCommand("select name,type from pc", oConnection)
 
P

phil

Hi, thanks for replying.
I forgot it indeed (i'm new to this).

But now, i get another error:
Compiler Error Message: BC30311: Value of type
'System.Data.OleDb.OleDbConnection' cannot be converted to
'System.Data.SqlClient.SqlConnection'.

Source Error:


Line 23: ' Ouverture de la connexion
Line 24: oConnection.Open()
Line 25: comd = New SqlCommand("select name,type from
pc",oConnection)
Line 26: dtreader = comd.ExecuteReader
Line 27: x = 0

Source File: D:\Inetpub\wwwroot\aspnet\Begin\Chapter07\access2.aspx Line:
25


This is in the beginning of the code:
<%@ Page Language="VB" %>
<%@ import namespace="System.Data"%>
<%@ import namespace="System.Data.SqlClient"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
....

Thanks again
Phil
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

You can't use an OleDbConnection with an SqlCommand. You have to use the
objects that fit together.

OleDbConnection
OleDbCommand
OleDbDataReader

or

SqlConnection
SqlCommand
SqlDataReader
 
P

phil

Yes, you are right ..
Thanks

Göran Andersson said:
You can't use an OleDbConnection with an SqlCommand. You have to use the
objects that fit together.

OleDbConnection
OleDbCommand
OleDbDataReader

or

SqlConnection
SqlCommand
SqlDataReader
 

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

Top