Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

CURD Data Manipulation in BsonDocument on No SQL Mongo Database ASP.Net

CURD Operations

CURD Data Manipulation is nothing but a Create Update Read and Delete. Normally those operation will be happened on the Relational Databases like SQL, Oracle and MySQL using the DDL and DML queries.

But in this chapter I am going to explain how we can achieve the CURD operations in Mongo DB in Asp.net.

Comparatively No Sql databases are more scalable and more efficient with relational database. Relational database will require schema for each creation of records but it doesn’t require any schema. Schema will be defined in runtime based on the data stored in the data collections.Large size of structured as well as unstructured data can be stored in NO SQL databases.

Easy to use the object oriented programing to store and retrieve the data from No Sql database.

Before creating the application we need to set up Mongo Database. Free mongo database setup are available in online, you can download and set it up in your local system or in server.

Create Asp.Net Web Form application.

curd net

Add the MongoDB assemblies from NUGet Packages.

curd net

Once you installed the Packages the respective DLLs will be added in your project reference.

curd net

I have created the Utility class for CURD operation as a separate class. Code snippets as bellow. Here is the BsonDocument used for CURD operation.

var documnt = new MongoDB.Bson.BsonDocument { {"ID","Cust001"}, {"Name","Raju"}, {"Addess","7B North Street"}, {"CIty","Chennai"}, {"Mobile","91xxxxxxxx"}, {"IsActive",true} };

Create a new custom class called Customer to do the CURD operation for the above document.

public class Customer { public string Id { get; set; } public string Name { get; set; } public string Address { get; set; } public string City { get; set; } public string Mobile { get; set; } public bool IsActive { get; set; } }

Create the new class in your project and copy paste the below code.

using MongoDB.Bson; using MongoDB.Driver; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace NOSQL { public class CustomerEntity { private IMongoClient Client; private IMongoDatabase MongoDB; private IMongoCollection < MongoDB.Bson.BsonDocument> Collec; public void SetConnection() { const string ConnectionString = "mongodb://127.0.0.1/?safe=true"; Client = new MongoClient(ConnectionString); MongoDB = Client.GetDatabase("Customer"); Collec = MongoDB.GetCollection < MongoDB.Bson.BsonDocument> ("computers"); } public void InsertAnsync(BsonDocument _cust) { Collec.InsertOneAsync(_cust); } public void Delete(string field, string value) { var whrdoc = Builders < BsonDocument> .Filter.Eq(field, value); Collec.DeleteMany(whrdoc); } public List < Customer> Display() { List < Customer> _cust = new List < Customer> (); using(var cursor = Collec.Find(new MongoDB.Bson.BsonDocument()).ToCursor()) { while (cursor.MoveNext()) { var batch = cursor.Current; foreach(var document in batch) { Customer _custitem = new Customer(); _custitem.Id = (document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[1].Value.ToString(); _custitem.Name = (document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[2].Value.ToString(); _custitem.Address = (document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[3].Value.ToString(); _custitem.City = (document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[4].Value.ToString(); _custitem.Mobile = (document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[5].Value.ToString(); _custitem.IsActive = Convert.ToBoolean((document.Elements as System.Collections.Generic.List < MongoDB.Bson.BsonElement> )[6].Value); // process document _cust.Add(_custitem); } } } return _cust; } public void Update(string Flfield, string Flvalue, string field1, string value1, string field2, string value2, string field3, string value3, string field4, string value4) { var whrdoc = Builders < BsonDocument> .Filter.Eq(Flfield, Flvalue); var update = Builders < BsonDocument> .Update.Set(field1, value1).Set(field2, value2).Set(field3, value3).Set(field4, value4); var result = Collec.UpdateOneAsync(whrdoc, update); } } }

Next step is to create the Web form design the Grid view with Edit template to do the CURD operation. Create new web Form and copy paste the below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="NOSQL.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> title tag - title </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" onrowcancelingedit="GridView1_RowCancelingEdit" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" CellSpacing="2"> <Columns> <asp:TemplateField HeaderText="ID"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Addess"> <EditItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Address") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Mobile"> <EditItemTemplate> <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FFF1D4" /> <SortedAscendingHeaderStyle BackColor="#B95C30" /> <SortedDescendingCellStyle BackColor="#F1E5CE" /> <SortedDescendingHeaderStyle BackColor="#93451F" /> </asp:GridView> </div> <asp:Button ID="Button1" runat="server" Text="Add Data" OnClick="Button1_Click" /> </form> </body> </html>

I have implemented different events in Gridview for CURD operations Create new record with BsonDocument

protected void Button1_Click(object sender, EventArgs e) { var documnt = new MongoDB.Bson.BsonDocument { { "ID", "Cust001" }, { "Name", "Raju" }, { "Addess", "7B North Street" }, { "CIty", "Chennai" }, { "Mobile", "91xxxxxxxx" }, { "IsActive", true } }; CustomerEntity custEntity = new CustomerEntity(); custEntity.SetConnection(); custEntity.InsertAnsync(documnt); refreshdata(); }

For Update the record we need to invoke two events both RowEditing and RowUpdating

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; refreshdata(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string CustID = GridView1.DataKeys[e.RowIndex].Values["id"].ToString(); string Name = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text; string Address = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text; string City = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text; string MobileNo = (GridView1.Rows[e.RowIndex].FindControl("TextBox5") as TextBox).Text; CustomerEntity custEntity = new CustomerEntity(); custEntity.SetConnection(); custEntity.Update("ID", CustID, "Name", Name, "Address", Address, "CIty", City, "Mobile", MobileNo); GridView1.EditIndex = -1; refreshdata(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; refreshdata(); } Delete Record protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { CustomerEntity custEntity = new CustomerEntity(); custEntity.SetConnection(); string id = GridView1.DataKeys[e.RowIndex].Values["id"].ToString(); custEntity.Delete("ID", id); refreshdata(); } Refresh Grid after doing the above operation. private void refreshdata() { CustomerEntity custEntity = new CustomerEntity(); custEntity.SetConnection(); GridView1.DataSource = custEntity.Display(); GridView1.DataBind(); }

NO SQL database is the most power full database architecture, which is more efficient and scalable database than our old relational database.

One of major advantage over relational database is, it is more flexible with object oriented programing Next is no need for schema like relational database, we can creation N number of collections, in each collections we can store the different schema of data.

In this Article I have explained the Mongo database integration with Asp.net Development. Data manipulation is happening through Grid view with different row events.

We hope this article will be useful and easy understandable.

Thanks for reading this article.

Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients