0

This is How I define my table as what this link: SQLite no such table error when table exists said

[Table("RegUserTable")]
[Serializable]
[DataContract]
public class RegUserTable
{
    [PrimaryKey]
    [DataMember]
    public Guid UserId { get; set; }

    [DataMember]
    public string Username { get; set; }

    [DataMember]
    public string Password { get; set; }

    [DataMember]
    public string Email { get; set; }

    [DataMember]
    public string Gender { get; set; }
}

This is my Login code:

public void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
    var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
    var db = new SQLiteConnection(dbpath);
    var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();

    if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
    {
        DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
    }
    else if (loginquery != null)
    {
        App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
    }
}

If I run this in my emulator it works 100%, but when I run it on my device it throws this error: enter image description here

What am I doing wrong here?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
paraJdox1
  • 805
  • 9
  • 23
  • 1
    For starters remove DataMember and Contract, it's not needed, secondly, make sure you are connecting to the correct database in case if you have multiple. Third show us the code where you are adding this table to you db! – FreakyAli Feb 08 '20 at 10:29
  • Thank you for saying the "Third" I just Added This var connection = new SQLiteAsyncConnection(dbpath); { connection.CreateTableAsync(); } now it works :D I did not add that at the beginning because my code (above) is just working fine in the emulator. – paraJdox1 Feb 08 '20 at 11:21
  • Added an answer kindly mark it for others who are looking for the same! – FreakyAli Feb 08 '20 at 12:21

1 Answers1

0

Creating the table something like below solves the issue

public async void Button_Clicked_1(object sender, EventArgs e)//LOGIN!
{
    var dbpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Userdatabase.db");
    var db = new SQLiteConnection(dbpath);

    var connection = new SQLiteAsyncConnection(dbpath); 
    await connection.CreateTableAsync<RegUserTable>();

    var loginquery = db.Table<RegUserTable>().Where(u => u.Username.Equals(EntryLoginUsername.Text) && u.Password.Equals(EntryLoginPassword.Text)).FirstOrDefault();

    if (string.IsNullOrWhiteSpace(EntryLoginUsername.Text) && string.IsNullOrWhiteSpace(EntryLoginPassword.Text))
    {
        DisplayAlert("Blank Fields", "Please Input Your Username and Password!", "OK");
    }
    else if (loginquery != null)
    {
        App.Current.MainPage = new NavigationPage(new MainPage(EntryLoginUsername.Text, GenderIdentifier.Text));
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
FreakyAli
  • 13,349
  • 3
  • 23
  • 63