ASP.Net C # CreateParameter with empty string

In my ASP.net C # web project, I have a request command object with parameters. I use the following code to populate 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 but empty lines. If I left the input field empty, it would pass the value as "". If this happens, I get the following exception:

ORA-01400: cannot insert NULL into (string)

Is there a way that allows me to insert empty rows into the database?

I am using an Oracle database and I am using System.Data.OracleClient as a provider.

+3
source share
3 answers

If you want to insert an empty string, you must allow NULL values. Otherwise, Oracle silently converts the empty string to NULL, and you get an exception.

- ' ', , .

, Oracle () : Oracle 9i NULL?

+3

null, param.Value = DBNull.Value, null:

if (value == null)
{
    param.Value = DBNull.Value;
}
else
{
    param.Value = value;
}
0

to try

param.Value = string.IsNullOrEmpty(value) ? DBNull.Value : value;
0
source

All Articles