Search code examples
c#asp.netoracle-databaseora-01400

ASP.Net C# CreateParameter with empty string


In my ASP.net C# web project I have a query command object that has parameters. I use the following code to fill the parameters:

DbCommand command = conn.CreateCommand();
command.CommandText = query; 
DbParameter param = command.CreateParameter();
param.ParameterName = parameter;
param.DbType = DbType.String;
param.Value = value;

This code works for all strings except for empty ones. If I would leave an input field blank, it would pass the value as "". If this happens I receive the following exception:

ORA-01400: cannot insert NULL into (string)

Is there a way that would allow me to insert blank strings into the database?

I use an Oracle database and I'm using System.Data.OracleClient as provider.


Solution

  • If you want to insert an empty string, you have to allow NULL values. Otherwise Oracle silently converts the empty string into NULL and you'll get the exception.

    Another option would be to insert an empty string with a space ' ' but i think that would be a pain.

    Here are further informations on why Oracle does it this (non standard) way: Why does Oracle 9i treat an empty string as NULL?