tags:

views:

247

answers:

3

I have a table in a MySql database that stores user accounts. One of the columns, expires, stores an expiration date but defaults to NULL. I need to be able to remove an expiration date and set it back to the default value.

Currently, all of my CRUD routines are written using MySqlCommand with parameters. Can this be done directly with a MySqlParameter, or do I have to create an alternate command object to handle this eventuality?

A: 

It's not clear what conditions you're talking about. If you want to set column to default value, you can use DbNull.Value;

command.AddWithValue("@param", DbNull.Value);

or

command.Parameters.Add("@param", <data type>).Value = DBNull.Value;
aku
A: 

Your second example was what I was already doing. command.Parameters.AddWithValue("@param", DBNull.Value) doesn't work either.

Both commands fail with the error:

There is no implicit conversion between 'System.DateTime' and 'System.DBNull'

Do I have to delete the row and reinsert it?

Adam Lassek
+1  A: 

The problem was DBNull, doing:

command.Parameters.AddWithValue("@parameter", null);

compiles OK.

Adam Lassek