Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# and mongoDB - Cannot deserialize from BsonType Null

I'm new to MongoDB and I've been religiously slogging through the Beginners' guide to using MongoDB 2.2 and the official C# driver http://www.codeproject.com/Articles/524602/Beginners-guide-to-using-MongoDB-and-the-offic

Everything seems to be going well, except I'm not sure how to handle a MongoDB Null value

This is my data structure

public class DataStructure
{
    public ObjectId _id { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
    public string ClaimId { get; set; }
    public string Status { get; set; }
    public string FmcCity { get; set; }
    public string FmcState { get; set; }
    public Int32 TestType { get; set; }
    public List<LineItemStructure> LineItems { get; set; }
}

public class LineItemStructure
{
    public string LineItemDescription { get; set; }
    public string LineItemType { get; set; }
    public string PartNumber { get; set; }
    public double LaborHours { get; set; }
    public double Quantity { get; set; }
}

And this is the code which connects to the MongoDB and pulls back values, until it encounters a null field value and returns the error below

       var client = new MongoClient(connectionString);
       MongoServer server = client.GetServer();
       var database = server.GetDatabase("FleetClaims");
      MongoCollection< DataStructure> collection = database.GetCollection< DataStructure >("Claims");
        //Execute the query
        MongoCursor< DataStructure> results = collection.FindAll();

"An error occurred while deserializing the LineItems property of class ...: An error occurred while deserializing the LaborHours property of class ... : Cannot deserialize Double from BsonType Null."

Can anyone suggest a reference or different approach to handle the nulls? Will BsonString.Empty help me here?

like image 216
Richard Avatar asked Aug 03 '16 21:08

Richard


1 Answers

public double? LaborHours { get; set; }
public double? Quantity { get; set; }

Allowed me to pass nulls into those fields.

like image 51
Richard Avatar answered Sep 27 '22 20:09

Richard