Reputation: 6188
I am getting this
Nullable object must have a value.
error in this line:
results.Test= installment.Test1.Value;
My 'Test' property looks like this:
[DataMember]
public int Test{ get; set; }
And my 'Test1' property looks like this in LINQ2SQL Designer:
public System.Nullable<int> Test1
{
get
{
return this._Test1;
}
set
{
if ((this._test1!= value))
{
this.OnTest1Changing(value);
this.SendPropertyChanging();
this._Test1= value;
this.SendPropertyChanged("Test1");
this.OnTest1Changed();
}
}
}
What am I doing wrong here?
Upvotes: 2
Views: 274
Reputation: 273244
Your Test1
property is (still) null
. The underlying field _Test1
will default to null
.
You can use
results.Test = installment.Test1 ?? 0;
if 0
is an acceptable default value.
Upvotes: 3
Reputation: 109
If you are going to use the value property it needs to have a value that is not null. You can check to see if the property has a value by using the HasValue property.
var test = (Test1.HasValue) ? (int?)Test1.Value : null;
You can use the variable directly and not use Value property.
var testing = Test1;
Upvotes: 0
Reputation: 1661
try this.
if (installment.Test1.HasValue) results.Test= installment.Test1.Value;
Upvotes: 0
Reputation: 46047
Try using this instead:
results.Test = installment.Test1.GetValueOrDefault(-1); //set default value
Upvotes: 1
Reputation: 108957
Looks like installment.Test1.Value
is null and cannot be assigned to int
Try
results.Test= installment.Test1 ?? 0;
Upvotes: 0
Reputation: 62246
The Value
of installment.Test1.Value
is a null
and you try to assign it to non nullable integer
type property Test
.
Upvotes: 0
Reputation: 48596
installment.Test1.Value;
Assumes that installment.Test1
actually has a value. The error you're getting, though, means that it doesn't, and that the property is actually null
. Is that unexpected?
Upvotes: 0