开发者

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 pass开发者_运维知识库ing 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 "" ?


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!


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜