Note that there are some explanatory texts on larger screens.

plurals
  1. POC# update a db entry with DBNull.value seems to error
    text
    copied!<p>Alright, maybe I am going around this wrong. We shall see.</p> <p>So there is some data I am checking. This data is going to be type Int32. More or less, if the item wasn't one of my flags, I wanted to set it as <code>DBNull.value</code>, I believe.</p> <p>The thing I am trying to determine is that the column is nullable, so if I don't enter anything into it, it shall be set to null.</p> <p>Here is my program logic:</p> <pre><code>int content = 5; //set for sake of program; int test; if( content == 5){ test = content; }else{ //i want test to be labeled as DBNULL. //didnt know if i could redefine here to be like: // free(test); DBNull test = new DBNull; } //my companys db command CommandFactory cmd = new CommandFactory(); cmd.commandText = "insert into TABLE ( value ) values ( @a )"; cmd.addInputParameters("@a", test, DBType.Int32); ... </code></pre> <p>In this case, how would is et it to dbnull? be like:</p> <pre><code>if(test != -1){ cmd.addInputParameters("@a", test, DBType.Int32); } //no param would be entered for @a, maybe forcing it to be null? </code></pre> <p><strong>In the definition of the table, value is nullable, so you wouldn't need to worry about that.</strong></p> <p><em>While the correct answer is posted, it is not the one I ended up using as my coworkers didn't want me to do "magic" with ??, so they had me do:</em></p> <pre><code>int? temp; if(temp.HasValue){ cmd.addInputParameters("@a", temp, DBType.Int32); }else{ cmd.addInputParameters("@a", DBNull.Value, DBType.Int32); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload