sql - Retrieve output parameter (identity column) from stored procedure with vb.net oledbcommand -
i trying retrieve output parameter value (identity field) stored procedure in sql server 2008 r2.
i looked @ following stackoverflow links. doing same still facing issues: retrieving output parameter stored procedure oledb command vb.net stored procedure - return identity output parameter or scalar
my stored procedure:
alter proc [dbo].[sp_custom_insertxref] @documentid int, @revnr int, @xrefdocument int, @xrefrevnr int, @xrefprojectid int, @refcount int, @xref int output begin set nocount on declare @hasfullpath bit=1; declare @relativepath nvarchar(300)=''; declare @refcountedit float=null; declare @reftimestamp datetime=getdate(); declare @xrtype int = 1; insert [epigrid].[dbo].[xrefs] ([documentid] ,[revnr] ,[xrefdocument] ,[xrefprojectid] ,[xrefrevnr] ,[hasfullpath] ,[relativepath] ,[refcount] ,[refcountedit] ,[reftimestamp] ,[xrtype]) values ( @documentid, @revnr, @xrefdocument, @xrefprojectid, @xrefrevnr, @hasfullpath, @relativepath, @refcount, @refcountedit, @reftimestamp, @xrtype ) set @xref=(select scope_identity()); --select @xref=(select scope_identity()); return @xref; end my vb.net code:
using connection new system.data.oledb.oledbconnection(connectionstring) connection.open() using command new oledbcommand("sp_custom_insertxref", connection) command.commandtype = commandtype.storedprocedure command.parameters.add("@documentid", oledbtype.integer, 4, parameterdirection.input).value = epdmpardoc.id command.parameters.add("@revnr", oledbtype.integer, 4, parameterdirection.input).value = epdmpardoc.getlocalversionno(parfolderid) command.parameters.add("@xrefdocument", oledbtype.integer, 4, parameterdirection.input).value = targetreplacedoc.id command.parameters.add("@xrefrevnr", oledbtype.integer, 4, parameterdirection.input).value = targetreplacedoc.currentversion command.parameters.add("@xrefprojectid", oledbtype.integer, 4, parameterdirection.input).value = parfolderid command.parameters.add("@refcount", oledbtype.integer, 4, parameterdirection.input).value = count 'command.parameters.add("@xref", oledbtype.integer, 4, parameterdirection.inputoutput).value = -1 command.parameters.add("@xref", oledbtype.integer) command.parameters("@xref").direction = parameterdirection.output command.executereader() xrefid = command.parameters("@xref").value end using connection.close() end using i tried direction output , return value , see same result. record created in database (no errors) code gets nothing output parameter. idea why?
when debug stored procedure in sql server management studio creates record , see return or output (tried both) correct value.
exec [dbo].[sp_custom_insertxref] @documentid =12, @revnr =1, @xrefdocument = 15, @xrefrevnr =1, @xrefprojectid =1, @refcount =2, @xref=-1; please advise.
first of why using execute reader function? if don't want return value stored use executenonquery() method instead of executereader(). if want read value stored procedure can use executereader() but, must have assigned datareader object.
for example
dim dr oledbdatareader = nothing try dr = command.executereader() if dr.hasrows while (dr.read()) var value = dr.getvalue(0) end while end if dr.close() catch 'oledb exception thrown here if dr isnot nothing if dr.isclosed = false dr.close() end if end try but, use above method should make changes in stored procedure.
replace following line:
set @xref=(select scope_identity()); --select @xref=(select scope_identity()); return @xref; to
select scope_identity(); after these output parameter not needed in stored procedure.
Comments
Post a Comment