Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mysql singleOrDefault Object of type 'System.Int32' cannot be converted to type 'System.String'. #30

Open
minus4 opened this issue Sep 3, 2009 · 1 comment

Comments

@minus4
Copy link

minus4 commented Sep 3, 2009

i have never posted here before so am unsure if everything i put is okay but here goes

when i use this code: var myparts = quote.All(); or singleordefault

i get an error of Object of type 'System.Int32' cannot be converted to type 'System.String'.

on Line 2371: quote single=null;
Line 2372: if(results.Count() > 0){
Line 2373: single=results.ToList()[0];
Line 2374: single.OnLoaded();
Line 2375: single.SetIsLoaded(true);

the activerecord
[code]
public partial class quote: IActiveRecord
{

    #region Built-in testing
    static TestRepository<quote> _testRepo;



    static void SetTestRepo(){
        _testRepo = _testRepo ?? new TestRepository<quote>(new stone_center.Models.stoneMySqlDB());
    }
    public static void ResetTestRepo(){
        _testRepo = null;
        SetTestRepo();
    }
    public static void Setup(List<quote> testlist){
        SetTestRepo();
        _testRepo._items = testlist;
    }
    public static void Setup(quote item) {
        SetTestRepo();
        _testRepo._items.Add(item);
    }
    public static void Setup(int testItems) {
        SetTestRepo();
        for(int i=0;i<testItems;i++){
            quote item=new quote();
            _testRepo._items.Add(item);
        }
    }

    public bool TestMode = false;


    #endregion

    IRepository<quote> _repo;
    ITable tbl;
    bool _isNew;
    public bool IsNew(){
        return _isNew;
    }

    public void SetIsLoaded(bool isLoaded){
        _isLoaded=isLoaded;
        if(isLoaded)
            OnLoaded();
    }

    public void SetIsNew(bool isNew){
        _isNew=isNew;
    }
    bool _isLoaded;
    public bool IsLoaded(){
        return _isLoaded;
    }

    List<IColumn> _dirtyColumns;
    public bool IsDirty(){
        return _dirtyColumns.Count>0;
    }

    public List<IColumn> GetDirtyColumns (){
        return _dirtyColumns;
    }

    readonly stoneMySqlDB _db;
    public quote(string connectionString, string providerName) {

        _db=new stone_center.Models.stoneMySqlDB(connectionString, providerName);
        Init();            
     }
    void Init(){
        TestMode=this._db.DataProvider.ConnectionString.Equals("test", StringComparison.InvariantCultureIgnoreCase);
        _dirtyColumns=new List<IColumn>();
        if(TestMode){
            quote.SetTestRepo();
            _repo=_testRepo;
        }else{
            _repo = new SubSonicRepository<quote>(_db);
        }
        tbl=_repo.GetTable();
        SetIsNew(true);
        OnCreated();       

    }

    public quote(){
         _db=new stone_center.Models.stoneMySqlDB();
        Init();            
    }


    partial void OnCreated();

    partial void OnLoaded();

    partial void OnSaved();

    partial void OnChanged();

    public IList<IColumn> Columns{
        get{
            return tbl.Columns;
        }
    }

    public quote(Expression<Func<quote, bool>> expression):this() {

        SetIsLoaded(_repo.Load(this,expression));
    }



    internal static IRepository<quote> GetRepo(string connectionString, string providerName){
        stone_center.Models.stoneMySqlDB db;
        if(String.IsNullOrEmpty(connectionString)){
            db=new stone_center.Models.stoneMySqlDB();
        }else{
            db=new stone_center.Models.stoneMySqlDB(connectionString, providerName);
        }
        IRepository<quote> _repo;

        if(db.TestMode){
            quote.SetTestRepo();
            _repo=_testRepo;
        }else{
            _repo = new SubSonicRepository<quote>(db);
        }
        return _repo;        
    }       

    internal static IRepository<quote> GetRepo(){
        return GetRepo("","");
    }

    public static quote SingleOrDefault(Expression<Func<quote, bool>> expression) {

        var repo = GetRepo();
        var results=repo.Find(expression);
        quote single=null;
        if(results.Count() > 0){
            single=results.ToList()[0];
            single.OnLoaded();
            single.SetIsLoaded(true);
            single.SetIsNew(false);
        }

        return single;
    }      

    public static quote SingleOrDefault(Expression<Func<quote, bool>> expression,string connectionString, string providerName) {
        var repo = GetRepo(connectionString,providerName);
        var results=repo.Find(expression);
        quote single=null;
        if(results.Count() > 0){
            single=results.ToList()[0];
        }

        return single;


    }


    public static bool Exists(Expression<Func<quote, bool>> expression,string connectionString, string providerName) {

        return All(connectionString,providerName).Any(expression);
    }        
    public static bool Exists(Expression<Func<quote, bool>> expression) {

        return All().Any(expression);
    }        

    public static IList<quote> Find(Expression<Func<quote, bool>> expression) {

        var repo = GetRepo();
        return repo.Find(expression).ToList();
    }

    public static IList<quote> Find(Expression<Func<quote, bool>> expression,string connectionString, string providerName) {

        var repo = GetRepo(connectionString,providerName);
        return repo.Find(expression).ToList();

    }
    public static IQueryable<quote> All(string connectionString, string providerName) {
        return GetRepo(connectionString,providerName).GetAll();
    }
    public static IQueryable<quote> All() {
        return GetRepo().GetAll();
    }

    public static PagedList<quote> GetPaged(string sortBy, int pageIndex, int pageSize,string connectionString, string providerName) {
        return GetRepo(connectionString,providerName).GetPaged(sortBy, pageIndex, pageSize);
    }

    public static PagedList<quote> GetPaged(string sortBy, int pageIndex, int pageSize) {
        return GetRepo().GetPaged(sortBy, pageIndex, pageSize);
    }

    public static PagedList<quote> GetPaged(int pageIndex, int pageSize,string connectionString, string providerName) {
        return GetRepo(connectionString,providerName).GetPaged(pageIndex, pageSize);

    }


    public static PagedList<quote> GetPaged(int pageIndex, int pageSize) {
        return GetRepo().GetPaged(pageIndex, pageSize);

    }

    public string KeyName()
    {
        return "quoteid";
    }

    public object KeyValue()
    {
        return this.quoteid;
    }

    public void SetKeyValue(object value) {
        if (value != null && value!=DBNull.Value) {
            var settable = value.ChangeTypeTo<string>();
            this.GetType().GetProperty(this.KeyName()).SetValue(this, settable, null);
        }
    }

    public override string ToString(){
        return this.quoteid.ToString();
    }

    public override bool Equals(object obj){
        if(obj.GetType()==typeof(quote)){
            quote compare=(quote)obj;
            return compare.KeyValue()==this.KeyValue();
        }else{
            return base.Equals(obj);
        }
    }

    public string DescriptorValue()
    {
        return this.quoteid.ToString();
    }

    public string DescriptorColumn() {
        return "quoteid";
    }
    public static string GetKeyColumn()
    {
        return "quoteid";
    }        
    public static string GetDescriptorColumn()
    {
        return "quoteid";
    }

    #region ' Foreign Keys '
    #endregion


    string _quoteid;
    public string quoteid
    {
        get { return _quoteid; }
        set
        {
            if(_quoteid!=value){
                _quoteid=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="quoteid");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    string _userid;
    public string userid
    {
        get { return _userid; }
        set
        {
            if(_userid!=value){
                _userid=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="userid");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    DateTime _datecreated;
    public DateTime datecreated
    {
        get { return _datecreated; }
        set
        {
            if(_datecreated!=value){
                _datecreated=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="datecreated");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    double _totalcost;
    public double totalcost
    {
        get { return _totalcost; }
        set
        {
            if(_totalcost!=value){
                _totalcost=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="totalcost");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    bool _ispaid;
    public bool ispaid
    {
        get { return _ispaid; }
        set
        {
            if(_ispaid!=value){
                _ispaid=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="ispaid");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    bool _depositpaid;
    public bool depositpaid
    {
        get { return _depositpaid; }
        set
        {
            if(_depositpaid!=value){
                _depositpaid=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="depositpaid");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    DateTime _templatedate;
    public DateTime templatedate
    {
        get { return _templatedate; }
        set
        {
            if(_templatedate!=value){
                _templatedate=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="templatedate");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    DateTime _deliverydate;
    public DateTime deliverydate
    {
        get { return _deliverydate; }
        set
        {
            if(_deliverydate!=value){
                _deliverydate=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="deliverydate");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }

    bool _completed;
    public bool completed
    {
        get { return _completed; }
        set
        {
            if(_completed!=value){
                _completed=value;
                var col=tbl.Columns.SingleOrDefault(x=>x.Name=="completed");
                if(col!=null){
                    if(!_dirtyColumns.Any(x=>x.Name==col.Name) && _isLoaded){
                        _dirtyColumns.Add(col);
                    }
                }
                OnChanged();
            }
        }
    }



    public DbCommand GetUpdateCommand() {
        if(TestMode)
            return _db.DataProvider.CreateCommand();
        else
            return this.ToUpdateQuery(_db.Provider).GetCommand().ToDbCommand();

    }
    public DbCommand GetInsertCommand() {

        if(TestMode)
            return _db.DataProvider.CreateCommand();
        else
            return this.ToInsertQuery(_db.Provider).GetCommand().ToDbCommand();
    }

    public DbCommand GetDeleteCommand() {
        if(TestMode)
            return _db.DataProvider.CreateCommand();
        else
            return this.ToDeleteQuery(_db.Provider).GetCommand().ToDbCommand();
    }


    public void Update(){
        Update(_db.DataProvider);
    }

    public void Update(IDataProvider provider){


        if(this._dirtyColumns.Count>0)
            _repo.Update(this,provider);
        OnSaved();
   }

    public void Add(){
        Add(_db.DataProvider);
    }



    public void Add(IDataProvider provider){


        var key=KeyValue();
        if(key==null){
            var newKey=_repo.Add(this,provider);
            this.SetKeyValue(newKey);
        }else{
            _repo.Add(this,provider);
        }
        SetIsNew(false);
        OnSaved();
    }



    public void Save() {
        Save(_db.DataProvider);
    }      
    public void Save(IDataProvider provider) {


        if (_isNew) {
            Add(provider);

        } else {
            Update(provider);
        }

    }



    public void Delete(IDataProvider provider) {


        _repo.Delete(KeyValue());

                }


    public void Delete() {
        Delete(_db.DataProvider);
    }


    public static void Delete(Expression<Func<quote, bool>> expression) {
        var repo = GetRepo();



        repo.DeleteMany(expression);

    }



    public void Load(IDataReader rdr) {
        Load(rdr, true);
    }
    public void Load(IDataReader rdr, bool closeReader) {
        if (rdr.Read()) {

            try {
                rdr.Load(this);
                SetIsNew(false);
                SetIsLoaded(true);
            } catch {
                SetIsLoaded(false);
                throw;
            }
        }else{
            SetIsLoaded(false);
        }

        if (closeReader)
            rdr.Dispose();
    }


} 

[/code]

the struct

[code]
public class quotesTable: DatabaseTable {

        public quotesTable(IDataProvider provider):base("quotes",provider){
            ClassName = "quote";
            SchemaName = "";


            Columns.Add(new DatabaseColumn("quoteid", this)
            {
                IsPrimaryKey = true,
                DataType = DbType.AnsiString,
                IsNullable = false,
                AutoIncrement = true,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("userid", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.AnsiString,
                IsNullable = true,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("datecreated", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.DateTime,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("totalcost", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.Double,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("ispaid", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.Byte,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("depositpaid", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.Byte,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("templatedate", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.DateTime,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("deliverydate", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.DateTime,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });

            Columns.Add(new DatabaseColumn("completed", this)
            {
                IsPrimaryKey = false,
                DataType = DbType.Byte,
                IsNullable = false,
                AutoIncrement = false,
                IsForeignKey = false,
                MaxLength = 0
            });



        }

        public IColumn quoteid{
            get{
                return this.GetColumn("quoteid");
            }
        }

        public static string quoteidColumn{
              get{
                return "quoteid";
            }
        }

        public IColumn userid{
            get{
                return this.GetColumn("userid");
            }
        }

        public static string useridColumn{
              get{
                return "userid";
            }
        }

        public IColumn datecreated{
            get{
                return this.GetColumn("datecreated");
            }
        }

        public static string datecreatedColumn{
              get{
                return "datecreated";
            }
        }

        public IColumn totalcost{
            get{
                return this.GetColumn("totalcost");
            }
        }

        public static string totalcostColumn{
              get{
                return "totalcost";
            }
        }

        public IColumn ispaid{
            get{
                return this.GetColumn("ispaid");
            }
        }

        public static string ispaidColumn{
              get{
                return "ispaid";
            }
        }

        public IColumn depositpaid{
            get{
                return this.GetColumn("depositpaid");
            }
        }

        public static string depositpaidColumn{
              get{
                return "depositpaid";
            }
        }

        public IColumn templatedate{
            get{
                return this.GetColumn("templatedate");
            }
        }

        public static string templatedateColumn{
              get{
                return "templatedate";
            }
        }

        public IColumn deliverydate{
            get{
                return this.GetColumn("deliverydate");
            }
        }

        public static string deliverydateColumn{
              get{
                return "deliverydate";
            }
        }

        public IColumn completed{
            get{
                return this.GetColumn("completed");
            }
        }

        public static string completedColumn{
              get{
                return "completed";
            }
        }


    }

[/code]

the sql for the table

CREATE TABLE IF NOT EXISTS quotes (
quoteid mediumint(9) NOT NULL auto_increment,
userid mediumint(9) default '0',
datecreated date NOT NULL,
totalcost float NOT NULL,
ispaid tinyint(1) NOT NULL default '0',
depositpaid tinyint(1) NOT NULL default '0',
templatedate date NOT NULL,
deliverydate date NOT NULL,
completed tinyint(1) NOT NULL default '0',
PRIMARY KEY (quoteid),
KEY userid (userid,datecreated,ispaid,depositpaid,templatedate,deliverydate,completed)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=63 ;

i cant use this table at all, so fix or advice on how to fix would be great

ta

@funky81
Copy link
Contributor

funky81 commented Sep 5, 2009

Have you downloaded the latest Subsonic 3 templates from github? Maybe you're runnin not the latest template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants