Here in my code DR.HasRows() returns True, but DR("LastID") returns DBNUll while SELECT IDENT_CURRENT ('SiteSection') AS 'LastID' works in SQL Management Studio.
__SQLString= "SELECT IDENT_CURRENT ('SiteSection') AS 'LastID'"
Dim DR As SqlDataReader
ddConnect() 'It's my custom class that opens a connection to db, it works
DR = ddExecuteReader()
DR.Read()
If DR.HasRows() Then
GetLastID = DR("LastID")
End If
I suspect that the account your .NET application is using does not have the same permissions as the account you are using to connect via Management Studio.
That said, I find your approach questionable at best. What are you planning to do with the information that comes from
IDENT_CURRENT()? If you think that you can take that and make assumptions about whatIDENTITYvalue is going to be generated for anINSERTthat you have not performed yet, please think again. Several factors can invalidate that assumption – someone can reseed the identity in the meantime, or change the increment, or insert a bunch of rows.So I suggest you reconsider what exactly you need to do and why you think
IDENT_CURRENTis the way to do it. Based on your comment, you should be sending:Now you can be sure that this is the ID you’ve inserted.