Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Sunday, November 17, 2019

Excluding fields in MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;                             
          
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void Exclude()
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

   var filter = Builders<BsonDocument>.Filter.Empty;   

 // uncomment the below two lines to exclude the required things

// var project = Builders<BsonDocument>.Projection.Exclude("_id"); //replace the field which you want to exclude in place of _id and it must match the field name in the MongoDB document

//var res = collection.Find<BsonDocument>(filter).Project(project).ToList();

    var res = collection.Find(filter).ToList(); //Comment this after un commenting the above two line

}

Before excluding _id field

After 



Saturday, November 16, 2019

Delete record from MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;   
                                    
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void DeleteOne(string id)
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

   var filter = Builders<BsonDocument>.Filter.Eq("_id",  BsonObjectId.Parse(id)); //Need to parse else you will get the error

   var res = collection.Find(filter).ToList();
 
   collection.DeleteOne(filter);
}

Filter record in MongoDB using Asp.net


using MongoDB.Bson;
using MongoDB.Driver;              
                         
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void ListOne(string age)
 {
    IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

    var filter = Builders<BsonDocument>.Filter.Eq("age", age);

    var res = collection.Find(filter).ToList();

 }

Get records from MongoDB using Asp.net.

To get the records from MongoDB using Asp.net.

using MongoDB.Bson;
using MongoDB.Driver;         
                              
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name
     
 public void List()
  {

    IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //table name

    var filter = Builders<BsonDocument>.Filter.Empty; //It behaves like select * from user

    var res = collection.Find(filter).ToList();
              
 }

Insert a record into MongoDB using Asp.net.


Below code shows how to insert a record into MongoDB using Asp.net.

First install the MongoDB driver from the nuget package manager.



using MongoDB.Bson;

using MongoDB.Driver; 

static MongoClient client = new MongoClient("mongodb://localhost:27017");//Set the connection string

        
static IMongoDatabase db = client.GetDatabase("test"); //Database name 


        public void InsertCollection()
        {
            BsonDocument doc = new BsonDocument  //Create BSON document object
            {
            { "name" , "me" },
            {"age" , "99"},
            {"addr" , new BsonDocument { {"pin" , "1234"} , {"state","unknown"} }}
            };

          IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user"); //Table name

            collection.InsertOne(doc);

        }

Sunday, October 29, 2017

Unable to update the EntitySet - because it has a DefiningQuery and no element exist

When you come across the above error while inserting into the database table using LINQ the reason for that is your Database table doesn't have a primary key I was trying to insert the data into the table which does not have any primary key created on it the solution is to create the primary key on that table.

Class Files inside the App_Code Folder not accessible

Just click on the properties of the class and change it's Build Action from content to compile.Please see the below images for clear idea.




Change to


Friday, October 27, 2017

.CS file not visible in Visual Studio


None of the .cs files are opening in the visual studio the solution for that is locate to the below folder at the location
C:\Users\My PC\AppData\Local\Microsoft\VisualStudio\12.0
Folder Name: -
ComponentModelCache
and delete the folder.If the message comes access is denied then check if any instance of the visual studio is running close it then delete the folder and check it worked for me.
  

Wednesday, December 21, 2016

Steps to uninstall windows service.

Open the Developer Command Prompt as administrator and locate to the same folder where the service has been created before


ex:-  E:\Test\WindowsService\bin\Release>
 

·         Then type the following command and press enter

  installutil /u <service name> 

Steps to install the windows service.

First build service in the release mode then copy the path where the service has been created inside the bin under the release folder

ex:- E:\Test\WindowsService\bin\Release 

you will find here the <service name>.exe file  that is your windows service if you are not able to see the <service name>.exe file there then you have to clean and rebuild your solution and then check it again.
·         Then open the Developer Command Prompt as the administrator locate the path  

ex:-  E:\Test\WindowsService\bin\Release>

·         Then type the command installutil.exe <service name> and press enter

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...