System.NullReferenceException occurred HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source= StackTrace: at mongotest.Program.Main(String[] args) in C:\Users\kiit\source\repos\C#-code\mongotest\mongotest\Program.cs:line 20
Please help me to solve this error. Why I get the null reference. Apart from this tell me how to get data in MongoDB for this schema
{
"_id" : ObjectId("5a78a7365a98c0a0d9118c1a"),
"name" : "Amit",
"contacts" : [
"userid1",
"userid2"
],
"logs" : {
"status" : "online",
"Available" : "False"
}
}
Code : how to set value for logs and contacts,I want to insert these value in database. What I will do with this solution: I will insert value in a database on click event in asp.net and take values from the user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace mongotest
{
class Program
{
static void Main(string[] args)
{
/* Connection in MongoDb and insert data in database */
var client = new MongoClient("mongodb://localhost");
var database = client.GetDatabase("SIH");
var collection = database.GetCollection<testing>("testing");
testing newobject = new testing();
newobject.name = "Amit Mishra";
newobject.logs.Available = "true";
newobject.logs.status = "false";
collection.InsertOneAsync(newobject);
Console.Write("Done");
Console.Read();
}
}
}
public class testing
{
public string _id { get; set; }
public string name { get; set; }
public string[] contacts { get; set; }
public Logs logs { get; set; }
}
public class Logs
{
public string status { get; set; }
public string Available { get; set; }
}