Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Null Boolean conundrun
Message
De
12/08/2014 17:04:35
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 5.0
OS:
Windows 8
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Divers
Thread ID:
01605517
Message ID:
01605523
Vues:
57
This message has been marked as the solution to the initial question of the thread.
>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


Try this:
newDataRow.SetField("Cmd_MasterStandBy", MasterStandBy);
or this
newDataRow["Cmd_MasterStandBy"] = (object)MasterStandBy ?? System.DBNull.Value;
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform