0
votes

I created a search bar with database connection. When i run it. It get an exception. i use a database that was created before.

Model class entries

public class entries
{
    public entries()
    {

    }

    public entries(string word)
    {
        this.word = word;
    }
    public entries(string word, string wordtype, string definition)
    {
        this.word = word;
        this.wordtype = wordtype;
        this.definition = definition;
    }   
    public string word
    { get; set; }

    public string wordtype
    { get; set; }

    public string definition
    { get; set; }

    public List<string> GetWord { get; set; }
}

Class DatabaseManager:

public class DatabaseManager
{
    SQLiteConnection dbConnection;
    public DatabaseManager()
    {
        dbConnection = DependencyService.Get<ISQLite>().GetConnection();
    }



    public List<string> GetWord()
    {
        return dbConnection.Query<string>("Select word From [entries]").ToList();
    }


}

MainPage.xaml.cs:

    DatabaseManager dbManager = new DatabaseManager();
 private void MySearchBar_SearchButtonPressed(object sender, EventArgs e)
        {

        }

        private void MySearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            var keyword = MySearchBar.Text;
            if(keyword.Length >= 1) { 
                var suggestions = dbManager.GetWord().Where(c => c.ToLower().Contains(keyword.ToLower()));

                SuggestionListView.ItemsSource = suggestions;
                SuggestionListView.IsVisible = true;
            }
            else
            {
                SuggestionListView.IsVisible = false;
            }
        }

This is Exception:

System.MissingMethodException: Constructor on type 'System.String' not found.

Please Help. Thank you so much.

1
'I'm new in ...' usually means 'I'm too lazy to use google'. Please do not start your posts like this. - Anton Belousov
which specific line throws the exception? - Jason
Thanks Jason!! This is the line that throws the exception: return dbConnection.Query<string>("Select word From [entries]").ToList(); - TanVuBoyGib
@TanVuBoyGib You would need to provide a class that has just one string property and then you can use Linq projection to transform those query results into a string list - SushiHangover

1 Answers

0
votes

You might have created an Entity for the “entries” table, say for example it is,

[Table (“entries”)]
public class Entries
{
    …

    [Column (“word”)]
    public string Word;

    …
}

then change the line

dbConnection.Query<string>("Select word From [entries]").ToList();

to

dbConnection.Table<Entries>().Select(x => x.Word).ToList();

This will remove the requirement of creating one additional class as Sushi said in above comment.

Moreover, following line will throw a NullReferenceException at c.ToLower(), if any of the word contains Null into the table.

dbManager.GetWord().Where(c => c.ToLower().Contains(keyword.ToLower()));

So to get rid of this, select results which has not null values like,

dbConnection.Table<Entries>().Where(x => x.Word != null).Select(x => x.Word).ToList();