Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I'm trying to use model objects to make my code less vulnerable but I'm having a problem with the syntax I believe.

In the Documentation for the Micron ORM Miron ORMDocumentation it displays the following example:

C#
MicronDbContext micron = new MicronDbContext();
//gets all customers from Nairobi city
 var customers = micron.GetRecords<Customer>(
                      new Customer()
                      {
                        City = "Nairobi"
                       }
                     );

  foreach (var customer in customers)
   {
     Console.WriteLine(customer.CustomerName);
   }


What I have tried:

This is what I've tried and the value keeps showing NULL. Is this a syntax error?

What i'm trying to achieve is the following sql command:
C#
//IEnumerable<Places> places = micron.GetRecords<Places>("SELECT * FROM `places` WHERE `id` = '" + frmPlaceList.chid + "'");


This is what is not working:

C#
var places = micron.GetRecords<Places>(
                     new Place()
                     {
                         id = frmPlaceList.chid
                     }
                    );
Posted
Updated 1-Jul-20 18:49pm

1 solution

1) I presume by 'not working' you mean there are no records returned ? it's good to be explicit here

2) The code you have certainly seems like a transliteration of their example and your data - obviously it builds, so syntax error I think 'not'

3) I guess/hope you've debugged and traced the value for 'id' coming from frmPlaceList.chid and that is a valid value in your database (I went through an example in SQL with someone here on CP recently who was looking for 'XYZ' all caps, but the data example used showed 'Xyz' - so it does happen)

4) You don't show the model definition of 'Places'/'Place' - eg their Customers model seems to be
/***CUSTOMER MODEL***/
  [Table("customers")]
 public partial class Customer : IMicron
 {
        [Primary]
        public Int32 CustomerID {get; set;}    // Your 'id' here is ???
        public String CustomerName {get; set;}
        public String ContactName {get; set;}
        public String Address {get; set;}
        public String City {get; set;}
        public String PostalCode {get; set;}
        public String Country {get; set;}
 }
so we have no way of knowing if your model actually contains an 'id' field - vs an 'ID' field etc etc

5) have you tried a 'get any record from your database, as in
MicronDbContext micron = new MicronDbContext();
 //get first record
 var firstPlace = micron.GetRecords<Places>().First();


6) and finally, from 5, if that shows a record returned, how about this
MicronDbContext micron = new MicronDbContext(); 

 var places = micron.GetRecords<Places>().Where(r=>r.id==frmPlaceList.chid);
 
Share this answer
 

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