Groups | Blog | Home
all groups > vb.net upgrade > april 2006 >

vb.net upgrade : return @@identity not working


Joemanc
4/21/2006 11:42:01 AM
I'm in the process of upgrading my app from VB6 to VB.net. One of the things
not working in .NET that was working in VB6 is returning the identity value.
I'm getting the error message 'Cast from type DBnull to integer is not valid'
with the code below:

Public Function SaveclsParty() As Integer
ptyID = 0
strsql = "exec sp_insert_PARTIES @ptyid = 0"
strsql = strsql & ", @sysid = " & sysid
strsql = strsql & ", @dmod = '" & dmod & "'"
strsql = strsql & ", @modby = '" & modby & "'"
strsql = strsql & ", @ptyname = '" & ptyname & "'"
strsql = strsql & ", @reltype = '" & reltype & "'"
strsql = strsql & ", @refattmt = '" & BoolStrg(refattmt) & "'"
strsql = strsql & ", @comments = '" & comments & "'"
strsql = strsql & ", @pstatus = " & pstatus
Call cn.Execute(strsql)

strsql = "select @@identity"

Rs = New ADODB.Recordset
Rs = cn.Execute(strsql)
SaveclsParty = Rs.Fields(0).Value 'erroring here

When I execute the insert statement in query analyzer, it inserts correctly
and also returns the identity as well, just not working in .NET. What do I
need to change?
Earl
4/22/2006 1:01:05 AM
Once SQL2k has executed your insert query, you then run another query to
retrieve the @@identity value that is long gone? It means what it says --
there is a null value being returned for the second query. You will need to
retrieve the @@Identity as part of your iniitial query. You could use an
output parameter to retrieve the @@Identity.


[quoted text, click to view]

Paul Clement
4/24/2006 8:05:25 AM
[quoted text, click to view]

¤ I'm in the process of upgrading my app from VB6 to VB.net. One of the things
¤ not working in .NET that was working in VB6 is returning the identity value.
¤ I'm getting the error message 'Cast from type DBnull to integer is not valid'
¤ with the code below:
¤
¤ Public Function SaveclsParty() As Integer
¤ ptyID = 0
¤ strsql = "exec sp_insert_PARTIES @ptyid = 0"
¤ strsql = strsql & ", @sysid = " & sysid
¤ strsql = strsql & ", @dmod = '" & dmod & "'"
¤ strsql = strsql & ", @modby = '" & modby & "'"
¤ strsql = strsql & ", @ptyname = '" & ptyname & "'"
¤ strsql = strsql & ", @reltype = '" & reltype & "'"
¤ strsql = strsql & ", @refattmt = '" & BoolStrg(refattmt) & "'"
¤ strsql = strsql & ", @comments = '" & comments & "'"
¤ strsql = strsql & ", @pstatus = " & pstatus
¤ Call cn.Execute(strsql)
¤
¤ strsql = "select @@identity"
¤
¤ Rs = New ADODB.Recordset
¤ Rs = cn.Execute(strsql)
¤ SaveclsParty = Rs.Fields(0).Value 'erroring here
¤
¤ When I execute the insert statement in query analyzer, it inserts correctly
¤ and also returns the identity as well, just not working in .NET. What do I
¤ need to change?

Sounds to me as if somewhere in your code you're attempting to assign a null value to an defined
integer variable. Can you identify on which line the error occurs?


Paul
~~~~
Joemanc
4/24/2006 12:34:03 PM
Paul - I'm erroring on this line:

SaveclsParty = Rs.Fields(0).Value 'erroring here

And you are correct, the value of rs.fields(0).value is null when I step
through.


[quoted text, click to view]
Joemanc
4/25/2006 6:35:01 AM
Paul - I'm trying to return the identity, the autonumber, the id of the
record I just inserted. Sure, I could check for NULL, but why would I want to
return 0?

Something definitely changed from VB6 to VB.NET as this code below worked in
VB6 and that is what I'm trying to figure out.

[quoted text, click to view]
Paul Clement
4/25/2006 7:27:35 AM
[quoted text, click to view]

¤ Paul - I'm erroring on this line:
¤
¤ SaveclsParty = Rs.Fields(0).Value 'erroring here
¤
¤ And you are correct, the value of rs.fields(0).value is null when I step
¤ through.

Then you should first check the value of the field in order to determine whether it is Null
(IsDbNull). If it is Null then return zero (or whatever numeric value you choose).


Paul
~~~~
Paul Clement
4/25/2006 1:35:12 PM
[quoted text, click to view]

¤ Paul - I'm trying to return the identity, the autonumber, the id of the
¤ record I just inserted. Sure, I could check for NULL, but why would I want to
¤ return 0?
¤
¤ Something definitely changed from VB6 to VB.NET as this code below worked in
¤ VB6 and that is what I'm trying to figure out.
¤

OK, I think I misunderstood the actual problem.

If you're using a stored procedure then you should be able to handle retrieval of the identity value
as well and return it to your app. See the following:

http://www.aspfaq.com/show.asp?id=2174


Paul
~~~~
Joemanc
4/27/2006 7:35:02 AM
Looks like my best bet is to convert my code to the new ado.net object model.

Hopefully that will take care of the identity issue.

[quoted text, click to view]
AddThis Social Bookmark Button