Alright, maybe I am going around this wrong. We shall see.
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 DBNull.value, I believe.
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.
Here is my program logic:
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);
...
In this case, how would is et it to dbnull? be like:
if(test != -1){
cmd.addInputParameters("@a", test, DBType.Int32);
}
//no param would be entered for @a, maybe forcing it to be null?
In the definition of the table, value is nullable, so you wouldn’t need to worry about that.
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:
int? temp;
if(temp.HasValue){
cmd.addInputParameters("@a", temp, DBType.Int32);
}else{
cmd.addInputParameters("@a", DBNull.Value, DBType.Int32);
}
How about: