MongoDB CRUD Operation In WPF C# With GridView

Building the sample

You have to install and configure it in your Windows system. Please make sure though MongoDB command prompt that it works.

For MongoDB installation and configuration on Windows sytem, you can go through

  • http://social.technet.microsoft.com/wiki/contents/articles/26542.mongo-db-installation-in-windows-7.aspx
  • http://prmdpandit.jbko.in/Archive/2014/9/mongo%20db%20installation%20in%20windows

or

Follow the steps given below.

MongoDB installation

  1. Go to http://www.mongodb.org/downloads Download 32bit zip file.
  2. After download, extract the file to c::/.
  3. Change <MongoDB 2.6 Standard> folder name to <MongoDB>
  4. Go to C:\MongoDb\ and create tow folder.

    1. Data
    2. Log

      1. mongodb.log(Text File)

  5. Go back int to the C:\mongodb \
    1. Create config file for MongoDB.

      <Mongo.config> create in the mongodb folder

  6. Open mongo.config file and paste this.

    dbpath=C:\mongodb\Data
    logpath=C:\mongodb\Log\mongodb.log
    diaglog=3

    Save the file.

  7. Go to the Wind+R or run and type cmd and open with an administrator.

    or

    Open DOS as an administrator.

  8. Change Directory c:\mongodb\bin\>
  9. c:\mongodb\bin\>mongod.exe --config="c:\mongodb\mongo.config" press enter

    WPF

  10. Open new command prompt with an administrator.
  11. Go to c::\mongodb\bin\> mongo.

    WPF

  12. Now, the Server will start and you are in MongoDB prompt.

    > use test
    >db

Description

Befor starting, please check your MongoDB Service, if it's running or not.

Follow the steps given below.

After installing MongoDB in your system, let's test some DB query in to MongoDB environment.

>Use mydb <press enter>

switch to my db

For other commands, please refer to the image given below.

WPF

This Application shows how the basic CRUD opration on MongoDB runs by using wpf C# and .NET with WPF GridView control.

In this Application, you have the functionality given below.

  1. You can configure the Local and server based mongodb configuration.
  2. Insert the new record in to the database collection.
  3. You can select the particular record.
  4. After selecting the particular record, you can update the selected record.
  5. After selecting the particular record, you can delete the selected record.
  6. You can select the record by, using the WPF GridView control etc.

It is immediately reflected in the GridView after any transaction on MongoDB by our Application.

You have to add MongoDB driver DLL for .NET.

WPF

To downlaod, .NET driver DLL, you can refer the link given below.

https://github.com/mongodb/mongo-csharp-driver/releases

I had already inserted it in to the debug folder but if you have any problem, you can get from there.

After the execution, the Application image is given below.

WPF

This is all the basic code, which I am using for simple CRUD operations in WPF C# and .NET, please go through the code.

C#

  1. using MongoDB.Bson;   
  2. using MongoDB.Driver;   
  3. using MongoDB.Shared;   
  4. using MongoDB.Bson.Serialization.Attributes;   
  5. using MongoDB.Driver.Builders;   
  6.    
  7. MongoClient mongo = new MongoClient("mongodb://localhost");   
  8. MongoServer server ;   
  9. MongoDatabase database;   
  10. MongoCollection<info> _infos;   
  11.    
  12. //This all the global variable in your project.   
  13.    
  14. //we will initiate this all in to the intialization method   
  15. server = mongo.GetServer();   
  16. server.Connect();    
  17. database= server.GetDatabase("mydb");   
  18.    
  19.    
  20. //"mydb" is the mongodb database name in my system //where database is there for curd opration.   
  21. //for insert the record in db    
  22.    
  23. public void addinfo(info _info)   
  24.         {   
  25.                 
  26.             _info.Id = ObjectId.GenerateNewId();   
  27.                
  28.             _infos.Insert(_info);   
  29.         }   
  30.    
  31. //For binding the gridview   
  32.    
  33. public void bindgrid()   
  34.         {   
  35.             // bind the existing info collection<table> record in grid   
  36.             _infos = database.GetCollection<info>("info");   
  37.             infogrid.DataContext = _infos.FindAll();   
  38.             infogrid.ItemsSource = _infos.FindAll();   
  39.         }   
  40.    
  41.    
  42. //update the data    
  43.    
  44. public void updateInfo(info _info)   
  45.        {   
  46.            IMongoQuery query = Query.EQ("info_id", _info.info_id);   
  47.            IMongoUpdate update = Update   
  48.                   
  49.                .Set("firstname", _info.firstname)   
  50.    
  51.               .Set("lastname", _info.lastname)   
  52.               .Set("age", _info.age);   
  53.            SafeModeResult result = _infos.Update(query, update);   
  54.                
  55.         }   
  56.    
  57. //delete the record   
  58.    
  59. IMongoQuery query = Query.EQ("info_id", _info.info_id);   
  60. SafeModeResult result = _infos.Remove(query); bindgrid();   

Source Code files

Model, which I have created for mapping of the info collection <table>

C#
  1. publicclass info {  
  2.     [BsonId]  
  3.     publicBson.BsonObjectId Id {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     publicint info_id {  
  8.         get;  
  9.         set;  
  10.     }  
  11.     publicstring firstname {  
  12.         get;  
  13.         set;  
  14.     }  
  15.     publicstring lastname {  
  16.         get;  
  17.         set;  
  18.     }  
  19.     publicint age {  
  20.         get;  
  21.         set;  
  22.     }  
  23. }