asp classic - SQL Server Stored Procedure SCOPE_IDENTITY() issue -


i have stored procedure , inside can see use 2 scope_identity(); problem second scope_identity assign @status variable when execute stored output @status variable null strange thing 2 inserts works fine.

i want return output of stored scope_identity of second insert. can me?

alter procedure [dbo].[sp_userinsert] ( @username   nvarchar(50)=null, @password   nvarchar(50)=null, @email      nvarchar(50)=null, @roleid     int = 0, @userid     int = 0, @typeop     int, @status     int = 0 output )     begin  set nocount on;  if (@typeop = 1) /*creazione nuovo recordo nella tabella utenti*/ begin  if exists(select * gruppi gruppi.groupid =@roleid)      begin         insert dbo.utenti(username,password,email) values(@username,@password,@email)         set @userid = scope_identity()         if (@userid > 0)             begin                 insert dbo.ruoli(userid,groupid) values(@userid,@roleid)                  set @status = @@rowcount             end       end  end   end 

now have problem if execute stored proc sql server management studio works fine if execute stored proc code works first insert! code:

 string connectionstring = configurationmanager.connectionstrings["sqlconnstr"].connectionstring;  void submitnewuser_click(object sender, eventargs e) {     string username = txtusername.text;     string email = txtemail.text;            string password = txtpassword.text;     int ddlroleid = convert.toint32(ddlroles.selectedvalue);      if (!string.isnullorwhitespace(username) && !string.isnullorwhitespace(email) && !string.isnullorwhitespace(password) && ddlroleid > 0)     {         if (checkusernameavailability(username))         {              try             {                 if (!string.isnullorwhitespace(connectionstring))                 {                     using (sqlconnection dbconn = new sqlconnection(connectionstring))                     {                         dbconn.open();                          sqlcommand command = new sqlcommand("sp_userinsert", dbconn);                         command.commandtype = commandtype.storedprocedure;                         command.parameters.addwithvalue("@username", username);                         command.parameters.addwithvalue("@password", password);                         command.parameters.addwithvalue("@email", email);                         command.parameters.addwithvalue("@roleid", 1);                         command.parameters.addwithvalue("@typeop", 1);                         command.executenonquery();                          dbconn.close();                     }                 }             }              catch (exception ex)             {                 // add error handling here debugging.                 // error message should not sent caller.                 system.diagnostics.trace.writeline("[validateuser] exception " + ex.message);             }         }     } } 

as far understand, don't have identity in second table. think, user_id, group_id unique key in table, can use

select * dbo.ruoil user_id = @user_id , group_id = @roleid 

if want check if there's rows affected, can use @@rowcount


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -