john
john

Reputation: 51

How to pass empty textbox data as parameter to stored procedure in oracle using .NET

I am a developing an application with different text boxs that I am passing to oracle stored procedure using ASP.NET ( c# ). For each textbox there is a property in a class file. When I pass empty textbox it is throwing an error. This is because that a varchar2 field in oracle is not accepting "null".

      Private string _fristName;

      public string FirstName 
      {
           get { return _fristName; }
           set { _fristName= value; }
      }

Which is best practice for making _fristName as string.empty or "" ?

Upvotes: 3

Views: 1666

Answers (2)

Jon Raynor
Jon Raynor

Reputation: 3892

Before setting the value of the parameter in the stored procedure, check for null. This could be put in a common function. Note this is for sql server, but a similiar function could be done for Oracle. Also, if the column can't take a null, then instead of DBNull.Value, maybe a default is used (String.Empty for strings, etc.).

IDbDataParameter parameter = new SqlParameter(ParameterName, DatabaseFieldType);

parameter.Value = (value== null ? DBNull.Value : value);

Upvotes: 0

David Hoerster
David Hoerster

Reputation: 28711

In your class' initialization, just set your private variable to a default value of String.Empty.

Private string _fristName = String.Empty;

Getting it will return an empty string, not null; setting it will change the value to whatever.

You could also do this:

  Private string _fristName;

  public string FirstName 
  {
       get { return _fristName ?? String.Empty; }
       set { _fristName= value; }
  }

Hope this helps!

EDIT: Took suggestion from @Marc in the comments. Changed the comparison to use the ?? operator. Thanks @Marc!

Upvotes: 5

Related Questions