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; }
}
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);
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.