Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQLite.NET-PCL – When I check for an existing user the function always return true even if the database table is empty

So, I create an async connection on the app start which creates a "Profiles.db3" in the "data/user/0/com.your.mypackage/files" (I checked, it exists and is empty)

Everytime I try to register as a new user it always tells me there’s an existing user

This is the method to check for an existing user:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public async Task<bool> CheckExistingUser(Profile profile)
        {
            await Init();
            var d1 = db.Table<Profile>().Where(x => x.Username == profile.Username || x.Email == profile.Email).FirstOrDefaultAsync(); 
            if (d1 != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

And I use it when creating a new profile:

public async Task<bool> NewProfile(User profile, string picture = "user.png")
        {
            await Init();
            if ((await CheckExistingUser(profile)))
            {
                return false;
            }
            else
            {
                Profile _profile = new Profile
                {
                    Username = profile.Username,
                    Email = profile.Email,
                    Password = profile.Password,
                    Picture = picture,
                    ProfileType = ProfileType.User,
                    AdditionalData = JsonConvert.SerializeObject(new
                    {
                        profile.BorrowedBooks,
                        profile.Wishlist,
                        profile.ReturnedBooks
                    })
                };
                await db.InsertAsync(_profile);
                return true;
            }
        }

This method always returns false since CheckExistingUser returns true.

This is to initiate the connection

public async Task Init()
        {
        
            if (db != null)
                return;


                db = DependencyService.Get<ISQLiteInterface>().GetSQLiteAsyncConnection();
                await db.CreateTableAsync<Profile>();
        }
            

I call it in the SignUp view model:

 if (await _profileService.NewProfile((User)Profile))
            {
                App.Current.MainPage = new AppShell();
            }

I am new to all this stuff so I don’t see where’s the problem since the logic seems correct to me.

>Solution :

Just change your code to:

public async Task<bool> CheckExistingUser(Profile profile)
        {
            await Init();
           //use the await keyword here... otherwise you get a task object
            var d1 = await db.Table<Profile>().Where(x => x.Username == profile.Username || x.Email == profile.Email).FirstOrDefaultAsync(); 
            if (d1 != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

The reason is always not null is because the call returns a task.

You need to await that task instead of just checking if the task is null or not.

You see you are calling FirstOrDefaultAsync which returns a task.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading