Skip to content

Instantly share code, notes, and snippets.

View guntidheerajkumar's full-sized avatar
💭
Happy

Dheeraj Kumar Gunti guntidheerajkumar

💭
Happy
View GitHub Profile
public partial class Employee
{
[Key]
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string Location { get; set; }
}
using MongoDB;
using MongoDB.Shared;
using MongoDB.Driver;
using MongodbExample.Models;
using MongoDB.Driver.Core;
private MongoClient client = new MongoClient("mongodb://localhost:27017");
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);
}
[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");
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));
}
}
public ActionResult Index()
{
try
{
int result = 100 / int.Parse("0");
}
catch (Exception ex)
{
Helper.ReportError(ex);
}
public class Employee
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
}
Employee emp = new Employee { FirstName = "Dheeraj", LastName = "Kumar", MiddleName = "Gunti" }
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());
}
@{
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>