nullable

NHibernate 2.* mapping files: how to define nullable DateTime type (DateTime?)?

I know one of the breaking changes with NHibernate 2.* is that the NHibernate.Nullables are no longer supported. Therefore, what do you use in your mapping file to map the nullable DateTime? type? For i.e.: Understandably doesn't work: <property name="CreateDate" column="CreateDate" type="DateTime?" not-null="false" /> And no longer ...

Nullable<T> confusion

Why is the following forbidden? Nullable<Nullable<int>> whereas struct MyNullable <T> { } MyNullable<Nullable<int>> is NOT ...

Dataset field DBNull -> int?

SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data in DatagridView OK. When trying to retrieve the data programmatically from the dataset the Dataset field retrieval code throws a StronglyTypedException error. [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] public int curr_read...

Nullable Method Arguments in C#

Duplicate Question http://stackoverflow.com/questions/271588/passing-null-arguments-to-c-methods/271600 Can I do this in c# for .Net 2.0? public void myMethod(string astring, int? anint) { //some code in which I may have an int to work with //or I may not... } If not, is there something similar I can do? ...

How can I constrain multiple columns to prevent duplicates, but ignore null values?

Here's a little experiment I ran in an Oracle database (10g). Aside from (Oracle's) implementation convenience, I can't figure out why some insertions are accepted and others rejected. create table sandbox(a number(10,0), b number(10,0)); create unique index sandbox_idx on sandbox(a,b); insert into sandbox values (1,1); -- accepted ins...

Which is preferred: Nullable<>.HasValue or Nullable<> == null?

I always used (a)Nullable<>.HasValue because I liked the semantics. However, recently I was working on someone else's existing code base where they used (b)Nullable<> == null exclusively instead. Is there a reason to use one over the other, or is it purely preference? (a) int? a; if(a.HasValue) ... (b) int? b; if(b != null) ...

Linq query with nullable sum problem

from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no vots are found with "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable valu...

.NET - Convert Generic Collection to DataTable

I am trying to convert a generic collection (List) to a DataTable. I found the following code to help me do this: // Sorry about indentation public class CollectionHelper { private CollectionHelper() { } // this is the method I have been using public static DataTable ConvertTo<T>(IList<T> list) { DataTable table = CreateTable<T>();...

How does the assignment of the null literal to a System.Nullable<T> type get handled by .Net (C#)?

I was wondering if anyone knows how the C# compiler handles the following assignment: int? myInt = null; My assumption is that there is an implicit conversion performed, but I cannot figure out how the null literal assignment is handled. I dissasembled the System.Nullable object and found the implicit operator is overriden to this: ...

Adding a column efficently in SQL Server

I want to add an integer column to a table with a large number of rows and many indexes (Its a data warehouse Fact Table). To keep the row width as narrow as possible all the columns in this table are defined as not null. So I want the new column to be not null with a default of zero. From experience adding this column will take some t...

Why won't a Nullable Public Property in a VB.net Class accept 0 (zero) as Assignment?

I have a nullable public property on a class using Vb.net 3.5: Public Property TicketCharge() As Nullable(Of Decimal) Get If _TicketCharge = Nothing Then Return Nothing Else Return _TicketCharge End If End Get Set(ByVal value As Nullable(Of D...

Nullable type is not a nullable type?

I was doing some testing with nullable types, and it didn't work quite as I expected: int? testInt = 0; Type nullableType = typeof(int?); Assert.AreEqual(nullableType, testInt.GetType()); // not the same type My question is why does testInt.GetType() return int, and typeof(int?) return the true nullable type? According to Enyra this ...

XmlConvert and nullable results?

In my project I receive an XmlElement of which I have to parse a few properties back to a class. For mapping these I use the XmlConvert class. But the source being XML there often are empty nodes or nodes that are not readable. Rather then throwing a bunch of errors, I want to get a NULL back to store in my class. I started making ...

How can I fix this up to do generic conversion to Nullable<T>?

I currently use this handy conversion extension method to do conversions between types: public static T To<T>(this IConvertible obj) { return (T)Convert.ChangeType(obj, typeof(T)); } However, it doesn't like converting valid values to Nullable, for example, this fails: "1".To<int?>(); Obviously, 1 is easily ...

Will this code break?

A business object is returning a short? datatype. How would I get this value into a variable? short? myShort = SomeBusinessObject.UserBlah; Is that correct? Will it break if it is null? ...

c#: assigning from nullable types

If I have a nullable "decimal? d" and I want to assign d to non nullable e, what is the proper way? ...

Why doesn't this C# code compile?

double? test = true ? null : 1.0; In my book, this is the same as if (true) { test = null; } else { test = 1.0; } But the first line gives this compiler error: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'double'. ...

Why shouldn't I always use nullable types in C#.

I've been searching for some good guidance on this since the concept was introduced in .net 2.0. Why would I ever want to use non-nullable data types in c#? (A better question is why wouldn't I choose nullable types by default, and only use non-nullable types when that explicitly makes sense.) Is there a 'significant' performance hit ...

Nullable types and the ternary operator. Why won't this work?

Just came across a weird error: private bool GetBoolValue() { //do some logic and return true or false } Then, in another method, something like this: int? x = GetBoolValue() ? 10 : null; Simple, if the method returns true, assign 10 to the Nullabale x, otherwise, assign null to the NULLABLE int. However, the compiler complains...

SqlParameter with Nullable value give error while ExecuteNonQuery?

I have an sql query that has a parameter that can be null in the database (Sql Server). The update method work fine until that user put a blank in the field, this produce a null value for the DataTime object (this object is nullable). The problem is when the dbCommand.ExecuteNonQuery();. Here is how I build the parameter for this field:...