3

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 "" ?

1
  • It's better to use string.Empty rather than "". After .Net 2.0, they are exactly the same. However, as far as readability is concerned, most programmers would agree that string.Empty is more pleasant to read. Commented Aug 8, 2011 at 19:04

2 Answers 2

5

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Or more tersely get { return _firstName ?? ""; }
Oooh, yeah -- I always forget about ??. Thanks!
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.