1

I have a problem. I am trying to get a value (string) from my database. Here is the code I am calling to get the value:

public string SelectValueFromTableSettings(string name)
{
    string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    try
    {
        using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
        {
            return connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).ToString();
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info("SQLiteEx", ex.Message);
        return null;
    }
}

And here is the code to assign the value to a variable:

string SwitchValue = MainActivity.db.SelectValueFromTableSettings(mItems[position].Name);

I am supposed to get a value like Nick or Steve, but I get this as value:

System.Collections.Generic.List`1[CryptoCurrencies.SettingDb]

What am I doing wrong?

5
  • Instead of enumerating the results of connection.Query<SettingDb>("..."), you're calling ToString() on it. connection.Query<SettingDb>() returns List<SettingDb>. Because there's no sensible stringification of List<T>, it just has the default ToString() which returns GetType().FullName or whatever. Instead, loop through the list. Commented Jun 11, 2019 at 17:49
  • What am I supposed to do then? Commented Jun 11, 2019 at 17:51
  • Loop through the list the query returns. See if there's anything in it. Commented Jun 11, 2019 at 17:51
  • 1
    when you call ToString() on Query result, you are getting the name of the result class which is a list of setting. Use FirstOrDefault() instead of ToString() to the first data in list and use SettingDb properties to get the desired result. Commented Jun 11, 2019 at 17:52
  • should you please post the SettingsDb class. Commented Jun 11, 2019 at 17:52

1 Answer 1

2

You are returning a list of objects.

connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).FirstOrDefault().YourProperty.ToString();

Or, simply return the object:

public SettingDB SelectValueFromTableSettings(string name)
{
    string folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    try
    {
        using (var connection = new SQLiteConnection(System.IO.Path.Combine(folder, "Settings.db")))
        {
            return connection.Query<SettingDb>("SELECT Value FROM SettingDb WHERE Name=?", name).FirstOrDefault();
        }
    }
    catch (SQLiteException ex)
    {
        Log.Info("SQLiteEx", ex.Message);
        return null;
    }
}

And then acess to the desired property.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped me a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.