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.
Add the MongoDB assemblies from NUGet Packages.
Once you installed the Packages the respective DLLs will be added in your project reference.
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" %>
title tag - title
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.