How can I write a method that returns all records from a Matisse table in a list structure?

StackOverflow https://stackoverflow.com/questions/16974346

  •  31-05-2022
  •  | 
  •  

Question

Very new to using Matisse so I don't know if I'm just flat out doing this wrong or something. I have a table containing usernames and passwords for the system, along with the following method...

    public List<Users> FindUsers()
    {

        List<Users> users = new List<Users>();
        //Users obj;
        executeCmd("SELECT * FROM Users");
        while (Reader.Read())
        {
            Users obj = new Users(db, 0x10ff);
            users.Add(obj);
        }
        reader.Close();
        return users;

    }

Which I had hoped would simply pull all records from the table, drop them in a list and return that list. The problem however lies with this line of code here.

    Users obj = new Users(db, 0x10ff);

I can only seem to pull one record, entering '0x10ff' as a parameter(the records OID) will return that record, which works with any record I try. Leaving it blank(i.e. '(db)' ) throws a Matisse exception(InvalidOperation) and entering anything else other than a specific records OID returns an ObjectNotFound exception. Am I just doing something totally stupid here? What am I missing?

Thanks

EDIT: db refers to the database connection, shown below, DBAcess being a class with connectivity & query functionality.

    private MtDatabase db;

    public DBAccess()
    {
        db = new MtDatabase(Properties.Settings.Default.Host, Properties.Settings.Default.Database);
        db.Open();
    }

And as for the constructers, I left them as the default ones when I imported the classes from Matisse

//  Generated constructor, do not modify
/// <summary>
/// The factory constructor
/// </summary>
/// <param name="db">a database</param>
/// <param name="mtOid">an existing object ID in the db</param>
public Users(MtDatabase db, int mtOid) : 
        base(db, mtOid) {
}

//  Generated constructor, do not modify
/// <summary>
/// Cascaded constructor, used by subclasses to create a new object in the database
/// </summary>
/// <param name="clsObj">the class descriptor of the class to instantiate</param>
protected Users(MtClass clsObj) : 
        base(clsObj) {
}


#endregion

//  GEN_END: Matisse Generated Code - Do not modify


//  Generated constructor
/// <summary>
/// Default constructor provided as an example. NOTE: You may modify or delete this constructor
/// </summary>
/// <param name="db">a database</param>
public Users(MtDatabase db) : 
        base(GetClass(db)) {
}
Was it helpful?

Solution

Have now sorted this out, turns out I was just doing it wrong.

Instead of trying to retrieve the values with the reader, if you instead return the object and then use that to create a new User containing the values of said object, that works. Not sure if that really explains it all that well so here is the updated, working code.

    public List<Users> FindUsers()
    {
        List<Users> users = new List<Users>();
        executeCmd("SELECT REF(Users) FROM Users c");
        while (Reader.Read())
        {
            MtObject obj = Reader.GetObject(0);
            users.Add(new Users(db, obj.MtOid));
        }
        Reader.Close();
        return users;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top