开发者

Trying to debug InvalidOperationException

My program keeps throwing an InvalidOperat开发者_JAVA百科ionException with the error "The null value cannot be assigned to a member with type System.Boolean which is a non-nullable value type". I can't make any sense of this because my code isn't trying to assign anything, simply iterate through a list and generate string of all claim numbers (or return an empty string if the enquiry has no associated claims):

public string ClaimNumbers
{
    get
    {
        if (Enquiry.Claims != null && Enquiry.Claims.Count>0)
        {
            var sb = new StringBuilder();
            foreach (var claim in Enquiry.Claims)
            {
                sb.Append(claim.ClaimId.ToString("00000") + ", ");
            }
            return sb.ToString().Substring(0, sb.Length - 2);
        }
        return string.Empty;
    }
}

The odd thing is that when I step through the code in the debugger and try to expand Enquiry.Claims.Count, I see:

Trying to debug InvalidOperationException

But then when I pause a bit and step onto the next line it changes to:

Trying to debug InvalidOperationException

I am using Linq to Sql, and Claims is an entity set, so I wondered if perhaps it was something to do with pulling the data from the database, however I'm not sure, so can't think what the best way to tackle fixing it is.


I assume that the CLAIMS table has a column that is mapped to a boolean property in your Claims entity and that column contains at least one NULL value.


This is most probably happens cause you use LINQ to SQL, or something like this, and on that exact line the resulting collection is null, which will force to LINQ engine to initialize returning value with null. So it throws an exception.

To resolve this problem, most likely, look how the Count of collection is going to be recovered and declare it nullable type with "?" symbol.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜