C# + Pl/SQL Problem with out parameter in stored procedure












2















I have a stored procedure which looks like this:



CREATE OR REPLACE PROCEDURE
Get_Username
(
p_Username IN user.USERNAME%TYPE,
p_ReturningUsername OUT user.Username%TYPE
)
IS
BEGIN
SELECT username INTO p_ReturningUsername FROM user WHERE p_Username = username;
EXCEPTION
WHEN NO_DATA_FOUND THEN
p_ReturningUsername := null;
END;


I've executed it from the database and it worked perfectly.
When I execute it from c# app I don't get any value back.



public bool getUsername(string username)
{
OracleConnection connection = getConnection();
OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
oracleCommand.CommandType = CommandType.StoredProcedure;
oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;
oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;

try
{
oracleCommand.ExecuteNonQuery();
}
catch (OracleException ex)
{
MessageBox.Show("Exception Message: " + ex.Message);
MessageBox.Show("Exception Source: " + ex.Source);
}


string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
connection.Close();
return tmp == username;
}


For a WPF registration window I need to check if the username is already taken.
When I execute this code I receive an empty string when it has to be the username in the user table.



Please help me, thanks in advance.










share|improve this question





























    2















    I have a stored procedure which looks like this:



    CREATE OR REPLACE PROCEDURE
    Get_Username
    (
    p_Username IN user.USERNAME%TYPE,
    p_ReturningUsername OUT user.Username%TYPE
    )
    IS
    BEGIN
    SELECT username INTO p_ReturningUsername FROM user WHERE p_Username = username;
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    p_ReturningUsername := null;
    END;


    I've executed it from the database and it worked perfectly.
    When I execute it from c# app I don't get any value back.



    public bool getUsername(string username)
    {
    OracleConnection connection = getConnection();
    OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
    oracleCommand.CommandType = CommandType.StoredProcedure;
    oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;
    oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;

    try
    {
    oracleCommand.ExecuteNonQuery();
    }
    catch (OracleException ex)
    {
    MessageBox.Show("Exception Message: " + ex.Message);
    MessageBox.Show("Exception Source: " + ex.Source);
    }


    string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
    connection.Close();
    return tmp == username;
    }


    For a WPF registration window I need to check if the username is already taken.
    When I execute this code I receive an empty string when it has to be the username in the user table.



    Please help me, thanks in advance.










    share|improve this question



























      2












      2








      2








      I have a stored procedure which looks like this:



      CREATE OR REPLACE PROCEDURE
      Get_Username
      (
      p_Username IN user.USERNAME%TYPE,
      p_ReturningUsername OUT user.Username%TYPE
      )
      IS
      BEGIN
      SELECT username INTO p_ReturningUsername FROM user WHERE p_Username = username;
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
      p_ReturningUsername := null;
      END;


      I've executed it from the database and it worked perfectly.
      When I execute it from c# app I don't get any value back.



      public bool getUsername(string username)
      {
      OracleConnection connection = getConnection();
      OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
      oracleCommand.CommandType = CommandType.StoredProcedure;
      oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;
      oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;

      try
      {
      oracleCommand.ExecuteNonQuery();
      }
      catch (OracleException ex)
      {
      MessageBox.Show("Exception Message: " + ex.Message);
      MessageBox.Show("Exception Source: " + ex.Source);
      }


      string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
      connection.Close();
      return tmp == username;
      }


      For a WPF registration window I need to check if the username is already taken.
      When I execute this code I receive an empty string when it has to be the username in the user table.



      Please help me, thanks in advance.










      share|improve this question
















      I have a stored procedure which looks like this:



      CREATE OR REPLACE PROCEDURE
      Get_Username
      (
      p_Username IN user.USERNAME%TYPE,
      p_ReturningUsername OUT user.Username%TYPE
      )
      IS
      BEGIN
      SELECT username INTO p_ReturningUsername FROM user WHERE p_Username = username;
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
      p_ReturningUsername := null;
      END;


      I've executed it from the database and it worked perfectly.
      When I execute it from c# app I don't get any value back.



      public bool getUsername(string username)
      {
      OracleConnection connection = getConnection();
      OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
      oracleCommand.CommandType = CommandType.StoredProcedure;
      oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;
      oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;

      try
      {
      oracleCommand.ExecuteNonQuery();
      }
      catch (OracleException ex)
      {
      MessageBox.Show("Exception Message: " + ex.Message);
      MessageBox.Show("Exception Source: " + ex.Source);
      }


      string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
      connection.Close();
      return tmp == username;
      }


      For a WPF registration window I need to check if the username is already taken.
      When I execute this code I receive an empty string when it has to be the username in the user table.



      Please help me, thanks in advance.







      c# sql oracle plsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 18:29







      BrainBox

















      asked Nov 14 '18 at 17:30









      BrainBoxBrainBox

      495




      495
























          2 Answers
          2






          active

          oldest

          votes


















          2














          As @Codo says, add the line command.BindByName = true;, but also replace the line



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;


          with



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2, 100, null, ParameterDirection.Output);


          Replace the number 100 with the number of characters in your username column.



          We use this overload of the Add method so that we can specify the size of the output parameter. This allows the Oracle driver to make enough space to receive the value from the database. The null argument is for the initial value of the parameter, which we don't care about because this is an output parameter, but we have to specify it as there is no overload of the Add method that takes only the name, type, size and direction.






          share|improve this answer
























          • Thanks a lot it worked, and now I understand how it works properly.

            – BrainBox
            Nov 14 '18 at 20:02



















          0














          Try this code (I've just swapped two lines):



          public bool getUsername(string username)
          {
          OracleConnection connection = getConnection();
          OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
          oracleCommand.CommandType = CommandType.StoredProcedure;
          oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;
          oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

          try
          {
          oracleCommand.ExecuteNonQuery();
          }
          catch (OracleException ex)
          {
          MessageBox.Show("Exception Message: " + ex.Message);
          MessageBox.Show("Exception Source: " + ex.Source);
          }


          string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
          connection.Close();
          return tmp == username;
          }


          Or add:



          oracleCommand.BindByName = true;


          By default, the OracleCommand class binds parameters by order, not by name.






          share|improve this answer
























          • That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

            – BrainBox
            Nov 14 '18 at 18:26













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305776%2fc-sharp-pl-sql-problem-with-out-parameter-in-stored-procedure%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          As @Codo says, add the line command.BindByName = true;, but also replace the line



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;


          with



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2, 100, null, ParameterDirection.Output);


          Replace the number 100 with the number of characters in your username column.



          We use this overload of the Add method so that we can specify the size of the output parameter. This allows the Oracle driver to make enough space to receive the value from the database. The null argument is for the initial value of the parameter, which we don't care about because this is an output parameter, but we have to specify it as there is no overload of the Add method that takes only the name, type, size and direction.






          share|improve this answer
























          • Thanks a lot it worked, and now I understand how it works properly.

            – BrainBox
            Nov 14 '18 at 20:02
















          2














          As @Codo says, add the line command.BindByName = true;, but also replace the line



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;


          with



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2, 100, null, ParameterDirection.Output);


          Replace the number 100 with the number of characters in your username column.



          We use this overload of the Add method so that we can specify the size of the output parameter. This allows the Oracle driver to make enough space to receive the value from the database. The null argument is for the initial value of the parameter, which we don't care about because this is an output parameter, but we have to specify it as there is no overload of the Add method that takes only the name, type, size and direction.






          share|improve this answer
























          • Thanks a lot it worked, and now I understand how it works properly.

            – BrainBox
            Nov 14 '18 at 20:02














          2












          2








          2







          As @Codo says, add the line command.BindByName = true;, but also replace the line



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;


          with



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2, 100, null, ParameterDirection.Output);


          Replace the number 100 with the number of characters in your username column.



          We use this overload of the Add method so that we can specify the size of the output parameter. This allows the Oracle driver to make enough space to receive the value from the database. The null argument is for the initial value of the parameter, which we don't care about because this is an output parameter, but we have to specify it as there is no overload of the Add method that takes only the name, type, size and direction.






          share|improve this answer













          As @Codo says, add the line command.BindByName = true;, but also replace the line



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;


          with



                  oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2, 100, null, ParameterDirection.Output);


          Replace the number 100 with the number of characters in your username column.



          We use this overload of the Add method so that we can specify the size of the output parameter. This allows the Oracle driver to make enough space to receive the value from the database. The null argument is for the initial value of the parameter, which we don't care about because this is an output parameter, but we have to specify it as there is no overload of the Add method that takes only the name, type, size and direction.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 19:40









          Luke WoodwardLuke Woodward

          45.1k126688




          45.1k126688













          • Thanks a lot it worked, and now I understand how it works properly.

            – BrainBox
            Nov 14 '18 at 20:02



















          • Thanks a lot it worked, and now I understand how it works properly.

            – BrainBox
            Nov 14 '18 at 20:02

















          Thanks a lot it worked, and now I understand how it works properly.

          – BrainBox
          Nov 14 '18 at 20:02





          Thanks a lot it worked, and now I understand how it works properly.

          – BrainBox
          Nov 14 '18 at 20:02













          0














          Try this code (I've just swapped two lines):



          public bool getUsername(string username)
          {
          OracleConnection connection = getConnection();
          OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
          oracleCommand.CommandType = CommandType.StoredProcedure;
          oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;
          oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

          try
          {
          oracleCommand.ExecuteNonQuery();
          }
          catch (OracleException ex)
          {
          MessageBox.Show("Exception Message: " + ex.Message);
          MessageBox.Show("Exception Source: " + ex.Source);
          }


          string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
          connection.Close();
          return tmp == username;
          }


          Or add:



          oracleCommand.BindByName = true;


          By default, the OracleCommand class binds parameters by order, not by name.






          share|improve this answer
























          • That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

            – BrainBox
            Nov 14 '18 at 18:26


















          0














          Try this code (I've just swapped two lines):



          public bool getUsername(string username)
          {
          OracleConnection connection = getConnection();
          OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
          oracleCommand.CommandType = CommandType.StoredProcedure;
          oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;
          oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

          try
          {
          oracleCommand.ExecuteNonQuery();
          }
          catch (OracleException ex)
          {
          MessageBox.Show("Exception Message: " + ex.Message);
          MessageBox.Show("Exception Source: " + ex.Source);
          }


          string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
          connection.Close();
          return tmp == username;
          }


          Or add:



          oracleCommand.BindByName = true;


          By default, the OracleCommand class binds parameters by order, not by name.






          share|improve this answer
























          • That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

            – BrainBox
            Nov 14 '18 at 18:26
















          0












          0








          0







          Try this code (I've just swapped two lines):



          public bool getUsername(string username)
          {
          OracleConnection connection = getConnection();
          OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
          oracleCommand.CommandType = CommandType.StoredProcedure;
          oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;
          oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

          try
          {
          oracleCommand.ExecuteNonQuery();
          }
          catch (OracleException ex)
          {
          MessageBox.Show("Exception Message: " + ex.Message);
          MessageBox.Show("Exception Source: " + ex.Source);
          }


          string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
          connection.Close();
          return tmp == username;
          }


          Or add:



          oracleCommand.BindByName = true;


          By default, the OracleCommand class binds parameters by order, not by name.






          share|improve this answer













          Try this code (I've just swapped two lines):



          public bool getUsername(string username)
          {
          OracleConnection connection = getConnection();
          OracleCommand oracleCommand = new OracleCommand("Get_Username", connection);
          oracleCommand.CommandType = CommandType.StoredProcedure;
          oracleCommand.Parameters.Add("p_Username", OracleDbType.Varchar2).Value = username;
          oracleCommand.Parameters.Add("p_ReturningUsername", OracleDbType.Varchar2).Direction = ParameterDirection.Output;

          try
          {
          oracleCommand.ExecuteNonQuery();
          }
          catch (OracleException ex)
          {
          MessageBox.Show("Exception Message: " + ex.Message);
          MessageBox.Show("Exception Source: " + ex.Source);
          }


          string tmp = oracleCommand.Parameters["p_ReturningUsername"].Value.ToString();
          connection.Close();
          return tmp == username;
          }


          Or add:



          oracleCommand.BindByName = true;


          By default, the OracleCommand class binds parameters by order, not by name.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 17:56









          CodoCodo

          51.1k11113150




          51.1k11113150













          • That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

            – BrainBox
            Nov 14 '18 at 18:26





















          • That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

            – BrainBox
            Nov 14 '18 at 18:26



















          That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

          – BrainBox
          Nov 14 '18 at 18:26







          That was my original code. I swapped it because I recive an exception executing this code: Exception Message: ORA-06502: PL/SQL: numeric or value error ORA-06512: at "W1O67X.GET_USERNAME", line 9 ORA-06512: at line 1 Exception Source: Oracle Data Provider for .NET, Managed Driver And I recive the same for the BindByName

          – BrainBox
          Nov 14 '18 at 18:26




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305776%2fc-sharp-pl-sql-problem-with-out-parameter-in-stored-procedure%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine