Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB 6.0 >>> VB.NET / April 2006

Tip: Looking for answers? Try searching our database.

return @@identity not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joemanc - 21 Apr 2006 19:42 GMT
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 - 22 Apr 2006 06:01 GMT
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.

> I'm in the process of upgrading my app from VB6 to VB.net. One of the
> things
[quoted text clipped - 27 lines]
> and also returns the identity as well, just not working in .NET. What do I
> need to change?
Paul Clement - 24 Apr 2006 14:05 GMT
¤ 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
~~~~
Microsoft MVP (Visual Basic)
Joemanc - 24 Apr 2006 20:34 GMT
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.

> ¤ 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.
[quoted text clipped - 30 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Paul Clement - 25 Apr 2006 13:27 GMT
¤ 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
~~~~
Microsoft MVP (Visual Basic)
Joemanc - 25 Apr 2006 14:35 GMT
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.

> ¤ Paul - I'm erroring on this line:
> ¤
[quoted text clipped - 10 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
Paul Clement - 25 Apr 2006 19:35 GMT
¤ 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
~~~~
Microsoft MVP (Visual Basic)
Joemanc - 27 Apr 2006 15:35 GMT
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.

> ¤ 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
[quoted text clipped - 14 lines]
> ~~~~
> Microsoft MVP (Visual Basic)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.