C# - SQL Server Stored Procedures - Reference? -


this question has answer here:

i working c#, sql server , writing web api application.

i creating sql server stored procedures , have several working, including inserting database, along updating entries in database. however, trying work outputs, , struggling simple.

i looking pass in email address, , return row id first match.

here stored procedure:

create procedure user_procedure_get_id     @email varchar(200),     @id int out     select *      [dbo].[users]     email = @email      set @id = id      return @id 

here c# code

public int getid( string emailaddress ) { // create sql command sqlcommand command = new sqlcommand(); command.connection = this.sqlconnection; // set command type use stored procedure command.commandtype = commandtype.storedprocedure; // name of stored procedure command.commandtext = "user_procedure_get_id";  // add email address command.parameters.add( new sqlparameter( "@email", emailaddress ) );  // add output param sqlparameter outputparam = new sqlparameter( "@id", sqldbtype.int ) {     direction = parameterdirection.output }; command.parameters.add( outputparam );  // create reader sqldatareader reader = null;  // execute command try {     reader = command.executereader(); } catch (invalidcastexception ex) { } catch (sqlexception ex)     { } catch (invalidoperationexception ex) { } catch (ioexception ex) { } {        reader.close();     }  debug.write( "user id: " + outputparam.value.tostring() );  return 0; } 

i aware am not handling exceptions , i'm not doing value returned. @ point don't value returned , error:

system.nullreferenceexception: object reference not set instance of object.

thanks in advance.

you need set @id in select clause , don't return either (it's out parameter).

create procedure user_procedure_get_id @email varchar(255), @id int out  select @id = id [dbo].[users] email = @email 

this doesn't need stored procedure though. query it.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -