Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm new to mongoDB and the help will be appreciated.
I created a class where I'm inserting data into collection using Entity class in a Windows form.
C#
namespace MongoDB_Setup
{
    private void btnInsertInDB_Click(object sender, EventArgs e)
    {        
          String connectionString = "mongodb://localhost";
          MongoClient client = new MongoClient(connectionString);
          MongoServer server = client.GetServer();
          MongoDatabase database = server.GetDatabase("NetCrawlerDemo");
          MongoCollection collection = database.GetCollection<SGATable>("SGATable");
          
          try
            {
              var SGATable1 = new SGATable()
                {
                    DA_TrackerID = ObjectId.GenerateNewId().Pid,
                    DA_Group = "Next word",
                    DA_SubGroup = "Over",                     
                    DA_DateTime = System.DateTime.Now
                };

                collection.Insert(SGATable1);
            }
catch(Exception ex){MessageBox.Show("Error: "+ex);}
}}

Entity Class
C#
namespace MongoDB_Setup
{
    class SGATable
    {        
        public ObjectId _id { get; set; }
        public Int32 DA_TrackerID { get; set; }
        public string DA_Group { get; set; }
        public string DA_SubGroup { get; set; }        
        public DateTime DA_DateTime { get; set; }
    }
}


Now I want to show all the inserted data in a gridView on FORM using a DataTable object. How to do it? Please help.
Posted
Comments
Nathan Minier 16-Oct-14 9:13am    
If you're trying to generate a DataTable from Mongo you're kind of missing the point. The collection is already in object form, use it as a native C# collection rather than translating it into a (faux) relational table in-memory.
GoneBump 16-Oct-14 12:44pm    
Hi, actually I need to retrieve the data from Database, as there is old data stored in DB. And I tried,
<pre>
MongoConnection mdb = new MongoConnection();
MongoDatabase database = mdb.MongoConnections("NetCrawlerDemo");
MongoCollection collection = database.GetCollection<"SGATable">("DA_TrackerInfo_SGA");

DataTable sg1 = new DataTable();
sg1 = collection.FindAllAs<"SGATable">();</pre>

but, it is not working as DataTable and collection can't be assigned.
Nathan Minier 17-Oct-14 7:23am    
That's how it should work, though, based on what a DataTable is. The DataTable is an in-memory representation of a Relational table, which a a Mongo collection is about the furthest thing from. Since DataTable is an intermediary step for a DataGridView, it can be eliminated altogether.

Now, you can bind data to a DataGridView off of any given IList; it should work for your collection result (but if it doesn't, we should be able to either cast it as a List or transfer it easily to one). Then you set your DataGridView.DataSource to the collection. It may take a little jiggling, but it should be relatively straightforward.

1 solution

I solved it myself, the code:

C#
String connectionString = "mongodb://localhost";
MongoClient client = new MongoClient(connectionString);
MongoServer mdb= client.GetServer();
MongoDatabase database = mdb.MongoConnections("NetCrawlerDemo");
MongoCollection collection = database.GetCollection<"SGATableFind">("DA_TrackerInfo_SGA");

MongoCursor<"BsonDocument"> cursor = collection.FindAllAs<"BsonDocument">();

DataTable dt = new DataTable();
dt.Columns.Add("_id", typeof(MongoDB.Bson.ObjectId));
dt.Columns.Add("DA_TrackerID", typeof(Int32));
dt.Columns.Add("DA_Group", typeof(string));
dt.Columns.Add("DA_SubGroup", typeof(string));
dt.Columns.Add("DA_SearchEngine", typeof(string));
dt.Columns.Add("DA_WebSiteURL", typeof(string));
dt.Columns.Add("DA_ArticleHeader", typeof(string));
dt.Columns.Add("DA_ArticleDescription", typeof(string));
dt.Columns.Add("DA_ArticleDetails", typeof(string));
dt.Columns.Add("DA_CompanyName", typeof(string));
dt.Columns.Add("DA_AdditionalKeyword", typeof(string));
dt.Columns.Add("DA_ArticleDateTime", typeof(string));
dt.Columns.Add("DA_ArticleYear", typeof(string));
dt.Columns.Add("DA_ArticleMonth", typeof(string));
dt.Columns.Add("DA_ArticleDay", typeof(string));
dt.Columns.Add("DA_DateTime", typeof(DateTime));

foreach (var item in cursor)
{
    dt.Rows.Add(item["_id"], item["DA_TrackerID"], item["DA_Group"], item["DA_SubGroup"], item["DA_SearchEngine"],
        item["DA_WebSiteURL"], item["DA_ArticleHeader"], item["DA_ArticleDescription"], item["DA_ArticleDetails"], item["DA_CompanyName"]
        , item["DA_AdditionalKeyword"], item["DA_ArticleDateTime"], item["DA_ArticleYear"], item["DA_ArticleMonth"], item["DA_ArticleDay"]
        , item["DA_DateTime"]);
}

grid1.DataSource = dt;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900