Returning SQL Server Output parameter to C# by Stored Procedure -


i have stored procedure in sql server 2012 output parameter. when call c# decrypt function, @ point when hit executenonquery(), error:

procedure or function 'decryptccode' expects parameter '@decryptedstr', not supplied.

how output value of stored procedure in code? thanks.

stored procedure:

alter procedure [dbo].[decryptccode]    @decryptedstr nchar(5) output  begin set nocount on;  if not exists      (select * sys.symmetric_keys symmetric_key_id = 101)     create master key encryption      password = 'rfsdffsssdfsdfwerefeses'  if not exists     (select * sys.certificates name='clientcert')       create certificate clientcert      subject = 'my clientcode certificate';  if not exists     (select * sys.symmetric_keys name='clientcode_k1')        create symmetric key clientcode_k1     algorithm = aes_256     encryption certificate clientcert;   open symmetric key clientcode_k1    decryption certificate clientcert;  select      @decryptedstr = convert(nvarchar, decryptbykey(clientcode, 1 , hashbytes('sha1', convert(varbinary, institutionid))))       dbo.lu_institution end 

c# code

public  string  decrypt() {     using (var cn = new sqlconnection(((entityconnection) objectcontext.connection).storeconnection.connectionstring))     {              try              {                  var sqlcmd = new sqlcommand("exec [dbo].[decryptccode]", cn);                  sqlcmd.parameters.add("@decryptedstr", sqldbtype.nchar, 5);                  sqlcmd.parameters["@decryptedstr"].direction = parameterdirection.output;                  cn.open();                  sqlcmd.executenonquery();                  cn.close();                   return sqlcmd.parameters["@decryptedstr"].value != dbnull.value ? (string)sqlcmd.parameters["@decryptedstr"].value : string.empty;              }              catch (exception e)              {                  cn.close();                  console.writeline(e.message);                  return string.empty;              }     } } 

your code looks fine, need specify command commandtype property, sql trying execute stored procedure.

public  string  decrypt()  {      using (var cn = new sqlconnection(((entityconnection) objectcontext.connection).storeconnection.connectionstring))      {          try          {                     cn.open();               var sqlcmd = new sqlcommand("[dbo].[decryptccode]", cn);               // specify command stored procedure              sqlcmd.commandtype = commandtype.storedprocedure;               sqlcmd.parameters.add("@decryptedstr", sqldbtype.nchar, 5);              sqlcmd.parameters["@decryptedstr"].direction = parameterdirection.output;               sqlcmd.executenonquery();                         return sqlcmd.parameters["@decryptedstr"].value != dbnull.value ? (string)sqlcmd.parameters["@decryptedstr"].value : string.empty;          }          catch (exception e)          {              console.writeline(e.message);              return string.empty;          }                   {             cn.close();          }      }  } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -