sql server - Using stored procedure output parameters in C# -


i having problem returning output parameter sql server stored procedure c# variable. have read other posts concerning this, not here on other sites, , cannot work. here have. trying print value comes back. following code returns null value. trying return primary key. have tried using @@identity , scope_indentity() (i.e. set @newid = scope_identity()).

stored procedure:

create procedure usp_insertcontract     @contractnumber varchar(7),      @newid int output begin      insert [dbo].[contracts] (contractnumber)         values (@contractnumber)      select @newid = id [dbo].[contracts] contractnumber = @contractnumber end 

opening database:

pvconnectionstring = "server = desktop-pc\\sqlexpress; database = pvdatabase; user id = sa;     password = *******; trusted_connection = true;";  try {     pvconnection = new sqlconnection(pvconnectionstring);     pvconnection.open(); } catch (exception e) {     databaseerror = true; } 

executing command:

pvcommand = new sqlcommand("usp_insertcontract", pvconnection);  pvcommand.transaction = pvtransaction; pvcommand.commandtype = commandtype.storedprocedure;      pvcommand.parameters.clear(); pvcommand.parameters.add(new sqlparameter("@contractnumber", contractnumber));  sqlparameter pvnewid = new sqlparameter(); pvnewid.parametername = "@newid"; pvnewid.dbtype = dbtype.int32; pvnewid.direction = parameterdirection.output; pvcommand.parameters.add(pvnewid);  try {     sqlrows = pvcommand.executenonquery();      if (sqlrows > 0)         debug.print("new id inserted =  ",              pvcommand.parameters["@newid"].value.tostring());      }     catch (exception e)     {         debug.print("insert exception type: {0}", e.gettype());         debug.print("  message: {0}", e.message);     } } 

i modified stored procedure (to use scope_identity) , looks this:

create procedure usp_insertcontract     @contractnumber varchar(7),     @newid int output begin     insert [dbo].[contracts] (contractnumber)     values (@contractnumber)      select @newid = scope_identity() end 

i tried , works fine (with modified stored procedure):

// define connection , command, in using blocks ensure disposal using(sqlconnection conn = new sqlconnection(pvconnectionstring )) using(sqlcommand cmd = new sqlcommand("dbo.usp_insertcontract", conn)) {     cmd.commandtype = commandtype.storedprocedure;      // set parameters     cmd.parameters.add("@contractnumber", sqldbtype.varchar, 7);     cmd.parameters.add("@newid", sqldbtype.int).direction = parameterdirection.output;      // set parameter values     cmd.parameters["@contractnumber"].value = contractnumber;      // open connection , execute stored procedure     conn.open();     cmd.executenonquery();      // read output value @newid     int contractid = convert.toint32(cmd.parameters["@newid"].value);     conn.close(); } 

does work in environment, too? can't why original code won't work - when here, vs2010 , sql server 2008 r2, works flawlessly....

if don't value - suspect table contracts might not have column identity property on it.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -