Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORA-06502: PL/SQL: numeric or value error: character string buffer too small exception from C# code

I am trying to execute some oracle pl/sql procedure with in and out parameters from # code on asp.net. I want to retrive the value from out parameter. but when I execute i am getting a oracle exception like "ORA-06502: PL/SQL: numeric or value error: character string buffer too small".What can I do now? Please help me.

code:

using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Activity.Account
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SuccessLabel.Visible = false;
            FailureLabel.Visible = false;
        }

        protected void Create_user(object sender, EventArgs e)
        {
            var id="";
            string oradb = "Data Source=OracleServerHost;User ID=scott;password=tiger";
            using (OracleConnection conn = new OracleConnection(oradb))
            {
                try
                {
                    OracleCommand cmd = new OracleCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "create_users_372640";
                    cmd.CommandType = CommandType.StoredProcedure;
                    OracleParameter p1 = new OracleParameter("u_user_id", UserIDTextBox.Text.TrimEnd());
                    OracleParameter p2 = new OracleParameter("u_First", FirstNameTextBox.Text.TrimEnd());
                    OracleParameter p3 = new OracleParameter("u_Last", LastNameTextBox.Text.TrimEnd());
                    OracleParameter p4 = new OracleParameter("u_Email", EmailIDTextBox.Text.TrimEnd());
                    OracleParameter p5 = new OracleParameter("u_password", PasswordTextBox.Text.TrimEnd());
                    cmd.Parameters.Add(p1);
                    cmd.Parameters.Add(p2);
                    cmd.Parameters.Add(p3);
                    cmd.Parameters.Add(p4);
                    cmd.Parameters.Add(p5);
                    OracleCommand cmd_chk = new OracleCommand();
                    cmd_chk.Connection = conn;
                    cmd_chk.CommandText = "check_user_372640";
                    cmd_chk.CommandType = CommandType.StoredProcedure;
                    OracleParameter p6 = new OracleParameter("user_id", UserIDTextBox.Text.TrimEnd());
                    cmd_chk.Parameters.Add(p6);
                    cmd_chk.Parameters.Add("id", OracleDbType.Varchar2).Direction = ParameterDirection.Output;
                    conn.Open();
                    cmd_chk.ExecuteNonQuery();
                    id=(string)cmd_chk.Parameters["id"].Value;
                    //OracleDataReader rd = cmd_chk.ExecuteReader();
                    if (id != null && id != "")
                    {
                        //rd.Read();
                        SuccessLabel.Visible = false;
                        FailureLabel.Visible = true;
                    }
                    else
                    {
                        cmd.ExecuteNonQuery();
                        SuccessLabel.Visible = true;
                        FailureLabel.Visible = false;    
                    }
                }catch(Exception){
                    Console.WriteLine("SQL Exception Occured");
                }
            }

        }
    }
}

My oracle procedure is:

create or replace procedure check_user_372640(
user_id in varchar2,
id out varchar2
)
as
u_id varchar2(20);
begin
select user_id into u_id from ACTIVITY_USERS_372640 where user_id=user_id;
id:=u_id;
end;
like image 298
Sandeep Reddy K. Avatar asked Feb 20 '14 06:02

Sandeep Reddy K.


People also ask

How to fix ORA-06502 PL SQL numeric or value error character string buffer too small?

ORA-06502: PL/SQL: numeric or value error: character string buffer too small. Cause: Variable length into clause v_variable1 is 5 character where output that trying to store in that variable is more than 5 character which cause the error. Solution: Increase the length of the variable v_variable1.

How to fix character string buffer too small?

Go to sql developer (Tools/Preferences/Database/NLS). Change it to 'CHAR' . To fix the issue need to recompile the invalid packages after changing the parameter to 'CHAR'. To check any other package's compiles incorrectly run this query .

How do you resolve a value or numeric error?

By either changing the numeric value to fall in the proper digit range, or by editing the variable to a different digit range to accommodate the value, the ORA-06502 will be resolved. This error message is generally a quick fix, but there are some ways you can expedite the process (or avoid the error altogether).

What is a character string buffer?

A thread-safe, mutable sequence of characters. A string buffer is like a String , but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads.


2 Answers

It is working for me now. Mistake is I have declared a parameter "Id" as varchar2. but I didn't give any size to that. Now I have declared max size to the parameter and its working fine.

 cmd_chk.Parameters.Add("id", OracleDbType.Varchar2,32767).Direction = ParameterDirection.Output;
like image 125
Sandeep Reddy K. Avatar answered Oct 22 '22 00:10

Sandeep Reddy K.


Another strage thing we RAN into related to this is with Oracle functions, for the special ParameterDirection.ReturnValue (*** all the rest of the ParameterDirection will work)

if you decalre it like bellow, directly in the constructor it DOSEN'T work:

cmd.Parameters.Add(new OracleParameter("myretval", OracleDbType.Long, 10, ParameterDirection.ReturnValue));

Result in error like:

ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-01403: no data found
ORA-06512: at line 1

if you declare it like this it works:

OracleParameter retval = (new OracleParameter("myretval", OracleDbType.Long, 10);
            retval.Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.Add(retval);
like image 32
sergiu Avatar answered Oct 22 '22 02:10

sergiu