Thursday, November 20, 2014

Retrieving Records from MongoDb and Displaying in GridView

We earlier discussed about Inserting the records to mongoDB via C#.
now the Next Step in the CRUD cycle is Read the data from MongoDb and Flash it on the UI screen.
This is how I did :

Drag and Drop the Gridview control and apply the styles which suites your website screen design


Create Connection like Before :
MongoClient monClient = new MongoClient("mongodb://localhost");
MongoServer ms = monClient.GetServer();



ms.Connect();
 
MongoDatabase db = ms.GetDatabase("testdb");
MongoCollection collection = db.GetCollection<Tasks>("Tasks");
Tasks tsk = new Tasks();

Query the collection and Convert to List
 
var ShowTasks = collection.FindAllAs<Tasks>().ToList<Tasks>();

Set the List as Data source
gvShowTasks.DataSource = ShowTasks;

Binds the Grid

gvShowTasks.DataBind();

Run the Solution.(f5) Bammmm!!! you see that
 

No comments:

Post a Comment