Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 I get scope_identity equal to 0

Author  Topic 

rtutus
Aged Yak Warrior

522 Posts

Posted - 2006-06-21 : 21:03:59
Although my row gets created correctly with the right value for the Identity field (100000063) I get a value of 0 for scope_identity. This is what I do:

I delcare my sqlCommand sqlcmdInsNeAcc and connection from system.data.Sqlclient, I open my connection. all goes well then I do:

sqlcmdInsNeAcc.CommandText = "set nocount on insert into TblAccount(AccRisAsse,AccHldBll,Acctaxexm)
values(@AccRisAsse,@AccHldBll,@Acctaxexm) select id=scope_identity()"
pm = sqlcmdInsNeAcc.Parameters.Add(New SqlParameter("@AccountNumber", SqlDbType.BigInt))
pm.Direction = ParameterDirection.ReturnValue
Dim kAccountNumber as integer
kAccountNumber = sqlcmdInsNeAcc.Parameters("@AccountNumber").Value

pm = sqlcmdInsNeAcc.Parameters.Add(New SqlParameter("@AccRisAsse", SqlDbType.Bit))
pm.Value = myvalue1
pm = sqlcmdInsNeAcc.Parameters.Add(New SqlParameter("@AccHldBll", SqlDbType.Bit))
pm.Value = myvalue2
pm = sqlcmdInsNeAcc.Parameters.Add(New SqlParameter("@Acctaxexm", SqlDbType.Bit))
pm.Value = myvalue3

sqlcmdInsNeAcc.executenonquery

then when I try to display the value of my returned scope identity from the variable: kAccountNumber
ALL I GET IS ZERO although my row is created in the table with the right Account number: 100000056

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2006-06-21 : 22:00:40
But.... @AccountNumber is not declared in your CommandText.....

Go to Top of Page

rtutus
Aged Yak Warrior

522 Posts

Posted - 2006-06-22 : 01:14:09
It is not, I just want it to get the scope identity. How do I put it in my command text since it's gonna be a returned value from SQL server
Thanks
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2006-06-22 : 01:27:16
Change your command text as..
sqlcmdInsNeAcc.CommandText = "set nocount on insert into TblAccount(AccRisAsse,AccHldBll,Acctaxexm)
values(@AccRisAsse,@AccHldBll,@Acctaxexm); select scope_identity()"

and execute it using executescalar as it will return single value
Go to Top of Page

rtutus
Aged Yak Warrior

522 Posts

Posted - 2006-06-22 : 11:00:31
moving kAccountNumber = sqlcmdInsNeAcc.Parameters("@AccountNumber").Value after executenonquery command resolved the pb perfectly
thanks
Go to Top of Page
   

- Advertisement -