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
Connection property has not been initialized
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)
-
- Posts: 12
- Joined: Wed 17 Sep 2008 22:25