Connection property has not been initialized

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
kevin.weir
Posts: 12
Joined: Wed 17 Sep 2008 22:25

Connection property has not been initialized

Post by kevin.weir » Mon 01 Jun 2009 19:21

When I call the following block of code I recieve a "Connection property has not been initialized" error when ExecuteScaler is called.

Dim db As Database = DatabaseFactory.CreateDatabase("MOEDB")
Dim sql As String = "SELECT SEQ_BUNDLES.NEXTVAL FROM DUAL"
Dim cmd As DbCommand = db.GetSqlStringCommand(sql)
Dim keyValue As Integer = CType(cmd.ExecuteScalar(), Integer)

If I modify the code to look like the foloowing it works:

Dim db As Database = DatabaseFactory.CreateDatabase("MOEDB")
Dim sql As String = "SELECT SEQ_BUNDLES.NEXTVAL FROM DUAL"
Dim cmd As DbCommand = db.GetSqlStringCommand(sql)
Dim connection As DbConnection = db.CreateConnection()
cmd.Connection = connection
cmd.Connection.Open()
Dim keyValue As Integer = CType(cmd.ExecuteScalar(), Integer)
cmd.Connection.Close()


My question is why isnt the connection suppoed to be created automatically when ExecuteScaler is called?

Thanks
Kevin

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Tue 02 Jun 2009 13:23

The DbCommand.ExecuteScalar() method doesn't create the connection object, but the DataBase.ExecuteScalar() method has this functionality. So please use the following code:

Code: Select all

Dim db As Database = DatabaseFactory.CreateDatabase("MOEDB")
Dim sql As String = "SELECT SEQ_BUNDLES.NEXTVAL FROM DUAL"
Dim keyValue As Integer = CType(db.ExecuteScalar(CommandType.Text, sql), Integer)

kevin.weir
Posts: 12
Joined: Wed 17 Sep 2008 22:25

Post by kevin.weir » Mon 08 Jun 2009 05:06

Thanks for reply. Its odd I didnt notice I was using cmd instead of db reference.

Post Reply