Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Null Boolean conundrun
Message
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
Null Boolean conundrun
Environment versions
Environment:
C# 5.0
OS:
Windows 8
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01605517
Message ID:
01605517
Views:
44
Hi All, (Not sure if Forum and Category are right, but I could not find c# as a forum or category)

I had a program working fine, until I decided I wanted to save null values into the database (which accepts nulls for the fields), the problem seems to be related to Boolean types, as it did not complain about other types (yet), for example I have something like this:
                public System.Boolean? MasterStandBy = null;
later on a Save method:
                System.Data.DataRow newDataRow = dataTable.NewRow();
...
                newDataRow["Cmd_MasterStandBy"] = MasterStandBy; 
...
                dataTable.Rows.Add(newDataRow);
And the column is defined as
            Columns.Add(new System.Data.DataColumn("Cmd_MasterStandBy", typeof(System.Boolean)));
I tried a lot of different options, this gives me the error "System.ArgumentException: Cannot set Column 'Cmd_MasterStandBy' to be null. Please use DBNull instead.", Now, I tried lots of different things, for example
                newDataRow["Cmd_MasterStandBy"] = MasterStandBy?? System.DBNull.Value; //Does not even compile
Change all Boolean types to be System.Data.SqlTypes.SqlBoolean?, tried typeof(System.Boolean?), typeof(System.Data.SqlTypes.SqlBoolean), typeof(System.Data.SqlTypes.SqlBoolean?) and many other things that I found googling, but I cannot solve this issue, what should I do?

TIA,
Hugo

PS. By the way, I use this table to update the SQL Server table using sqlBulkCopy, and it was working great when I initialized all the values to default{T} (false, zero etc), problems started when I initialized the values to null, and apparently only with the Boolean type
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Next
Reply
Map
View

Click here to load this message in the networking platform