Connection property has not been initialized

Connection property has not been initialized

Postby 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
kevin.weir
 
Posts: 12
Joined: Wed 17 Sep 2008 22:25

Postby 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)
Shalex
Devart Team
 
Posts: 7473
Joined: Thu 14 Aug 2008 12:44

Postby kevin.weir » Mon 08 Jun 2009 05:06

Thanks for reply. Its odd I didnt notice I was using cmd instead of db reference.
kevin.weir
 
Posts: 12
Joined: Wed 17 Sep 2008 22:25


Return to dotConnect for Oracle