Error when get Table data

May 18, 2012 at 9:32 PM

public class DataManager : PropertyChangedCore
    {
        private Database database;
        private Table<Person> current;
        public Table<Person> Current
        {
            get { return current; }
            set
            {
                current = value; OnPropertyChanged("TablePerson");
            }
        }

        public DataManager()
        {
            Data();
        }

        public async void Data()
        {
           var exists = await Database.DoesDatabaseExistsAsync("Hello", StorageLocation.Local);
            if (!exists)
            {
                database = await Database.CreateDatabaseAsync("Hello", StorageLocation.Local);
                database.CreateTable<Person>("TablePerson");
                var table = await database.Table<Person>("TablePerson");
                await database.SaveAsync();
                table.AddRange(new Person[] 
                { 
                    new Person() 
                    { 
                        Name = "sdfsdfs"
                    }, 
                    new Person() 
                    { 
                        Name = "sfsdfsfdds"
                    } 
                });
                await database.SaveAsync();
            }
            else
            { 
                database = await Database.OpenDatabaseAsync("Hello", true, StorageLocation.Local);
                Current = await database.Table<Person>("TablePerson");
            }
        }

        public List<Person> GetPerson()
        {
            List<Person> persons = new List<Person>();
            foreach (var p in Current) //Here I have error becouse Current = null. Why?
            {
                persons.Add(p);
            }
            return persons;
        }
    }

 

When I create db I add two persons. I don't understand why Current is empty.  Help me please to resolve my problem.

Jun 29, 2012 at 3:22 AM

It does not look like you are setting Current anywhere when new DB is created.

If you would like to email me a sample project, I can take a closer look.

Thanks.