I
Israel
I found that if I use parameters on my insert statement and then
execute "select SCOPE_IDENTITY()" it always returns DbNull. However
if I DO NOT use parameters in the insert statement then I get the id
back correctly. Also if I use parameters and use "select @@identity"
it works fine? Note that I am using two different SqlCommand objects
that are using the same connection and transaction.
So this seems like some crazy bug but for some reason I can't find any
real answers about it. It appears that this guy had the same issue a
year ago but no one really answered the question:
http://groups.google.com/group/micr...=gst&q=SCOPE_IDENTITY+DbNull#272deab74ae2ff28
Here are two methods that demonstrate the problem:
Private Function InsertData1(ByVal SQLcon As SqlConnection, ByVal
transaction As SqlTransaction) As Decimal
Dim insCmd As SqlCommand
insCmd = New SqlCommand("insert into table_1 (name, value) values
(@name, @value);", SQLcon, transaction)
insCmd.Parameters.Add("@name", SqlDbType.NVarChar)
insCmd.Parameters.Add("@value", SqlDbType.Int)
insCmd.Parameters("@name").Value = "Israel"
insCmd.Parameters("@value").Value = 666
insCmd.ExecuteNonQuery()
Dim siCmd As SqlCommand
siCmd = New SqlCommand("select SCOPE_IDENTITY()", SQLcon,
transaction)
Dim objId = siCmd.ExecuteScalar()
Dim Id As Decimal = 0
If IsDBNull(objId) Then
Throw New Exception("Bad Monkey!") ' ALWAYS GET HERE
Else
Id = DirectCast(objId, Decimal)
End If
Return Id
End Function
Private Function InsertData2(ByVal SQLcon As SqlConnection, ByVal
transaction As SqlTransaction) As Decimal
Dim insCmd As SqlCommand
insCmd = New SqlCommand("insert into table_1 (name, value) values
('Israel', 666);", SQLcon, transaction)
insCmd.ExecuteNonQuery()
Dim siCmd As SqlCommand
siCmd = New SqlCommand("select SCOPE_IDENTITY()", SQLcon,
transaction)
Dim objId = siCmd.ExecuteScalar()
Dim Id As Decimal = 0
If IsDBNull(objId) Then
Throw New Exception("Bad Monkey!")
Else
Id = DirectCast(objId, Decimal) ' ALWAYS GET HERE
End If
End Function
execute "select SCOPE_IDENTITY()" it always returns DbNull. However
if I DO NOT use parameters in the insert statement then I get the id
back correctly. Also if I use parameters and use "select @@identity"
it works fine? Note that I am using two different SqlCommand objects
that are using the same connection and transaction.
So this seems like some crazy bug but for some reason I can't find any
real answers about it. It appears that this guy had the same issue a
year ago but no one really answered the question:
http://groups.google.com/group/micr...=gst&q=SCOPE_IDENTITY+DbNull#272deab74ae2ff28
Here are two methods that demonstrate the problem:
Private Function InsertData1(ByVal SQLcon As SqlConnection, ByVal
transaction As SqlTransaction) As Decimal
Dim insCmd As SqlCommand
insCmd = New SqlCommand("insert into table_1 (name, value) values
(@name, @value);", SQLcon, transaction)
insCmd.Parameters.Add("@name", SqlDbType.NVarChar)
insCmd.Parameters.Add("@value", SqlDbType.Int)
insCmd.Parameters("@name").Value = "Israel"
insCmd.Parameters("@value").Value = 666
insCmd.ExecuteNonQuery()
Dim siCmd As SqlCommand
siCmd = New SqlCommand("select SCOPE_IDENTITY()", SQLcon,
transaction)
Dim objId = siCmd.ExecuteScalar()
Dim Id As Decimal = 0
If IsDBNull(objId) Then
Throw New Exception("Bad Monkey!") ' ALWAYS GET HERE
Else
Id = DirectCast(objId, Decimal)
End If
Return Id
End Function
Private Function InsertData2(ByVal SQLcon As SqlConnection, ByVal
transaction As SqlTransaction) As Decimal
Dim insCmd As SqlCommand
insCmd = New SqlCommand("insert into table_1 (name, value) values
('Israel', 666);", SQLcon, transaction)
insCmd.ExecuteNonQuery()
Dim siCmd As SqlCommand
siCmd = New SqlCommand("select SCOPE_IDENTITY()", SQLcon,
transaction)
Dim objId = siCmd.ExecuteScalar()
Dim Id As Decimal = 0
If IsDBNull(objId) Then
Throw New Exception("Bad Monkey!")
Else
Id = DirectCast(objId, Decimal) ' ALWAYS GET HERE
End If
End Function