This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class Employee | |
{ | |
[Key] | |
public int EmployeeId { get; set; } | |
public string EmployeeName { get; set; } | |
public string Location { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using MongoDB; | |
using MongoDB.Shared; | |
using MongoDB.Driver; | |
using MongodbExample.Models; | |
using MongoDB.Driver.Core; | |
private MongoClient client = new MongoClient("mongodb://localhost:27017"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ActionResult> Index() | |
{ | |
var database = client.GetDatabase("Employee"); | |
var collection = database.GetCollection<Employee>("employee"); | |
var data = await collection.Find<Employee>(_ => true).ToListAsync(); | |
return View(data); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost] | |
public async Task<ActionResult> Create(Employee employee) | |
{ | |
if(ModelState.IsValid) | |
{ | |
var database = client.GetDatabase("Employee"); | |
var collection = database.GetCollection<Employee>("employee"); | |
await collection.InsertOneAsync(employee); | |
} | |
return RedirectToAction("Index"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Helper | |
{ | |
public static void ReportError(this Exception ex, [CallerMemberName] string callmember = "", | |
[CallerFilePath] string callfilepath = "", [CallerLineNumber] int callLineNumber = 0) | |
{ | |
Trace.WriteLine(string.Format("[{0:g} - {1} - {2} - line {3}] {4}", DateTime.UtcNow.ToLocalTime(), callmember, | |
callfilepath, callLineNumber, ex.Message)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public ActionResult Index() | |
{ | |
try | |
{ | |
int result = 100 / int.Parse("0"); | |
} | |
catch (Exception ex) | |
{ | |
Helper.ReportError(ex); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Employee | |
{ | |
public string FirstName { get; set; } | |
public string MiddleName { get; set; } | |
public string LastName { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Employee emp = new Employee { FirstName = "Dheeraj", LastName = "Kumar", MiddleName = "Gunti" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using (MemoryStream stream = new MemoryStream()) | |
{ | |
//Creating an default serialization instance of specific class type | |
var serializer = SerializationContext.Default.GetSerializer<Employee>(); | |
//Using Pack method serializing object to bytes and adding them to a stream. | |
serializer.Pack(stream, emp); | |
//Using Unpack or UnpackSingleObject, stream can be deserialized back to object. | |
var unpack = serializer.UnpackSingleObject(stream.GetBuffer()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
Layout = null; | |
} | |
<link href="~/Content/bootstrap.css" rel="stylesheet" /> | |
<div class="container"> | |
<div class="alert alert-danger" role="alert" style="margin-top:10px;"> | |
<h1>Error Page... Something Went Wrong</h1> | |
<h4>Please Try Again. If Still Persists, Please Contact System Administrator.</h4> |