c# - How can I return a value as an OUTPUT parameter? -
i'm trying output parameter work, whatever reason, it's refusing to. keep getting null result. if on sql side, works fine. tested this,
declare @test int exec myproc @number = 1, @id = @test print @test which gave me output expected. i've looked on code hour , looks right. cause id10t error, brain isn't seeing it.
public static int myfunc(int id) { using (var connection = new sqlconnection(configurationmanager.connectionstrings["cnstring"].connectionstring)) { connection.open(); using (var cmd = new sqlcommand("dbo.myproc", connection) { commandtimeout = 120, commandtype = commandtype.storedprocedure }) { cmd.parameters.addwithvalue("@number", sqldbtype.int); var param = new sqlparameter("@id", sqldbtype.int) { direction = parameterdirection.output }; cmd.parameters.add(param); cmd.executenonquery(); debug.writeline(param.value); return convert.toint32(param.value); } } } alter procedure dbo.myproc ( @number int , @id int output ) begin set nocount on; set @id = ( select id sometable somevalue = @number ); return; end;
you have simple typo ...
you passing value of sqldbtype.int expecting instead of parameter myfunc.
cmd.parameters.addwithvalue("@number", sqldbtype.int); should be
cmd.parameters.addwithvalue("@number", id);
Comments
Post a Comment