1

So i just got into programming and into C# & noSQL databases. I'm currently working on a small program, that shows the data of a MongoDB database on an ASP.NET webapplication. Im stuck with connecting to my local database.

The data needs to be stored in lblMotherboard: This is my code. builder.aspx:

<asp:Label ID="lblMotherboard" runat="server" Text=""></asp:Label>

builder.aspx.cs:

using MongoDB.Bson;
using MongoDB.Driver;

    public void showMobos_Click(object sender, EventArgs e)
    {
        var name = "";
        var connectionString = "mongodb://localhost:27017/";
        var mongoClient = new MongoClient(connectionString);
        MongoServer server = mongoClient.GetServer(); 
        MongoDatabase database = server.GetDatabase("mydb");

        MongoCollection<Post> mobos = database.GetCollection<Post>("moederborden");
        foreach (Post parts in mobos.FindAll())
        {
            name = name + " " + parts.Aanbieder + " " + parts.ProductNaam; 
        }
        lblMotherboard.Text = name; 
    }

showMobos_Click starts when a button is clicked. It doesn't print any data. What is going wrong?

Sample document:

{
    "_id" : ObjectId("54a287ef0d0a7f888510d14e"),
    "Aanbieder" : "Coolblue",
    "Productlink" : "http://computerstore.nl/product/470130/category-208983/asrock-z97-extreme6.html",
    "Productnaam" : "Asus H97-Pro Gamer",
    "Prijs" : "129,-",
    "Socket" : "1150"
} 

Post.class:

public class Post
{
    public ObjectId _id { get; set; }
    public String Aanbieder { get; set; }
    public String Productlink { get; set; }
    public String Productnaam { get; set; }
    public decimal Prijs { get; set; } 
    public int Socket { get; set; } 
}
10
  • Is there any exception? or mobos is empty? Commented Dec 30, 2014 at 18:22
  • When i load up the application and click inspect element it says in the console: Failed to load resource: the server responded with a status of 404 (Not Found). Mobos isn't empty. Commented Dec 30, 2014 at 18:26
  • So the problem has nothing to do with mongo, your usage of mongo driver is ok Commented Dec 30, 2014 at 18:28
  • So what could be the problem then ? Commented Dec 30, 2014 at 18:49
  • debug your code, do you enter your showMobos_Click method? try a simple insert into mobos collection and see if you get a 'MongoDB.Driver.MongoConnectionException' or not. Commented Dec 30, 2014 at 18:59

1 Answer 1

2

You have to change the Prijs to a number

Run this in mongo console:

db.moederborden.find().forEach(function(doc)
{
    var price = doc.Prijs.replace(',', ''); // it may be vary for your other document
    price = price.replace('-', '');    
    doc.Prijs = Number(price);

    db.moederborden.update({_id : doc._id} , doc);
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.