Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a varchar field with null value

Tags:

c#

mysql

I have a field clube varchar(50) that is by default NULL, and it it's not notnull so it accepts null values. What I need to know is, if I have a value already on this field and I want to make an Update and set it's value with null, how would I do that?
I tried to set null the value of a string but didnt worked.

IF some condition become true, I want to set this field as NULL by an Update.

And as obvious, if the condition become false, I will pass a value and NOT null value. So I need to do it using MySqlcommand Parameter. Like:

sql = "UPDATE usuarios SET nome = @nome";
cmd.CommandText = sql;
cmd.Parameter.Add(New MySqlParameter("@nome", MySqlDbType.VarChar)).Value = `variable`;

if the condition is true = variable = null else variable != null.

like image 296
Ghaleon Avatar asked Mar 25 '13 19:03

Ghaleon


1 Answers

To set the value to null:

cmd.Parameter.Add(New MySqlParameter("@nome", MySqlDbType.VarChar)).Value = DBNull.Value;
like image 130
Joe Frambach Avatar answered Oct 06 '22 08:10

Joe Frambach