[RESOLVED]Asp.net MVC connection with Mongodb Null reference exception

my home controller constructor and index method

public MongoDatabase mongodatabase;
        public HomeController()
        {
           // var connectionString = "mongodb://localhost/db";
            //var client = new MongoClient(connectionString);
            var client = new MongoClient(Settings.Default.lovenmaryConnectionstring);
            var server = client.GetServer();
            var database = server.GetDatabase(Settings.Default.db);

            //MongoUrl url = new MongoUrl(connectionString);
            //MongoClient client = new MongoClient(url);
            //var dbName = url.DatabaseName; // retrive database name
            //var db = client.GetServer().GetDatabase(dbName);

        }

        public ActionResult Index()
        {
            mongodatabase.GetStats();

            return Json(mongodatabase.Server.BuildInfo, JsonRequestBehavior.AllowGet);
        }

here on 

mongodatabase.GetStats();
showing error
in settings mongodb://localhost

Hi pjpatil,

Thanks for your post.

Double check you have the following connection string in your config file,like this:

<add name="Mongo" connectionString="mongodb://localhost/yourDatabase" />

There’s a tutorial,you can check this:http://www.joe-stevens.com/2011/10/02/a-mongodb-tutorial-using-c-and-asp-net-mvc/

If there’s anything else I can do for you regarding this matter,please feel free to post back in this forum.

Best Regards,

Eileen

Leave a Reply