Bind Data to ASP.Net gridview using MongoDB

I have created a simple demonstration program to show how we can bind data from Mongo to gridview.

Here we go through code…

Create Database Connection

/*create separate for connection object */

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Configuration;

using System.Data.SqlClient;

using MongoDB.Driver;

using MongoDB.Driver.Builders;

using MongoDB.Bson;

namespace Gridcontrol.Mongodb

{

public static class MongoDBConnection

{

static string connectionString=”mongodb://localhost/?safe=true;w=1;wtimeout=30s”;

static List<BsonDocument> bsonlist = new List<BsonDocument>();

public static MongoDatabase getDatabase()

{

SafeMode safemode = SafeMode.True;

MongoServer server = MongoServer.Create(connectionString);

MongoDatabase database = server.GetDatabase(“mydb”);

return database;

}

}

}

Insertion and Retrieval of data From MongoDB

/*create another class for set and get data from Mongo*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using MongoDB.Driver;

using MongoDB.Driver.Linq;

using MongoDB.Driver.Builders;

using MongoDB.Bson;

using Gridcontrol.Mongodb;

namespace Gridcontrol.Mongodb

{

public class Mongodb

{

public void insertProduct()

{

MongoCollection collection =     MongoDBConnection.getDatabase().GetCollection<Product>(“productlist”);

/*Remove the records if collection already exsists*/

if (collection.Count() > 0)

collection.RemoveAll();

for (int i = 1; i <= 10; i++)

{

Product product = new Product()

{

ProductName = “Datacard” + i,

PurchaseDate = System.DateTime.Now.ToString(),

Price = 100 * i

};

collection.Save(product);

}

}

public List<Product> getMongoData()

{

insertProduct();

MongoCollection collection =  MongoDBConnection.getDatabase().GetCollection<Product>(“productlist”);

IEnumerable<Product> query = from e in collection.AsQueryable<Product>()

select new Product

{

Id = e.Id,

ProductName = e.ProductName,

PurchaseDate = e.PurchaseDate,

Price = e.Price

};

return query.ToList();

}

public class Product

{

public ObjectId Id { get; set; }

public string ProductName { get; set; }

public string PurchaseDate { get; set; }

public float Price { get; set; }

}

}

}

Bind the Data to GridView

/*In default.aspx.cs Page load event write this code*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Gridcontrol.Mongodb;

namespace Gridcontrol

{

public partial class _Default : System.Web.UI.Page

{

//create object for mongodb class which created previously and call getmongodata

// Method to bind data to gridview

Gridcontrol.Mongodb.Mongodb mdb = new Mongodb.Mongodb();

protected void Page_Load(object sender, EventArgs e)

{

gvmongo.DataSource = mdb.getMongoData();

gvmongo.DataBind();

}

}

}

/*In Default.aspx page add gridview control*/

<%@ Page Title=”Home Page” Language=”C#” MasterPageFile=”~/Site.master” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”Gridcontrol._Default” %>

<asp:Content ID=”HeaderContent” runat=”server” ContentPlaceHolderID=”HeadContent”>

</asp:Content>

<asp:Content ID=”BodyContent” runat=”server” ContentPlaceHolderID=”MainContent”>

<div>

</div>

<div>

<asp:GridView ID=”gvmongo” runat=”server” AutoGenerateColumns=”false” GridLines=”None” HeaderStyle-BackColor=”Green” AlternatingRowStyle-BackColor=”Wheat”

Width=”800px” Height=”300px” HeaderStyle-ForeColor=”White”>

<Columns>

<asp:TemplateField HeaderText=”Product Name” ItemStyle-ForeColor=”Blue” HeaderStyle-HorizontalAlign=”Left”>

<ItemTemplate><%#Eval(“ProductName”)%></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=”Purchase Date” HeaderStyle-HorizontalAlign=”Left”    ItemStyle-ForeColor=”Blue”>

<ItemTemplate><%#Eval(“PurchaseDate”) %></ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText=”Price” HeaderStyle-HorizontalAlign=”Left” ItemStyle-ForeColor=”Blue”>

<ItemTemplate><%#Convert.ToDouble(Eval(“Price”))%></ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</asp:Content>

Thank You!                        

Advertisements

2 comments

  1. Its really helpful…Keep posting..

  2. Good post…… it helped me alot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: