Forum Cross Platform with Xamarin

Hello Guys, i am trying to display a single record on a label from sqlite db, but is not working.

Here is my code.

namespace LangaLanga.Pages
{
public partial class Alphabets : ContentPage
{
private SQLiteConnection database;
public alphabets result;
private static object collisionLock = new object();

   // public ObservableCollection<Tbl_Alphabets> alphabets { get; set; }
    String val;
    int ID;
    public Alphabets()
    {
        InitializeComponent();
        database = DependencyService.Get<ISQLite>().GetConnection();
        // database.CreateTable<Tbl_Alphabets>();
        //this.alphabets = new ObservableCollection<Tbl_Alphabets>(database.Table<Tbl_Alphabets>());

    }

    public void GetAlphabets()
    {

        result =  database.Query<alphabets>("select * from Tbl_Alphabets where Alpabet_ID = 1").FirstOrDefault();
        result = new alphabets();
        ID = result.Alpabet_ID;  
    }

    public void GetNext()
    {

        result = database.Query<alphabets>("select Alpabet_ID, * from Tbl_Alphabets where Alpabet_ID = " + ID + "+1").FirstOrDefault();
        result = new alphabets();
        ID = result.Alpabet_ID;
    }

    public void GetPrevious()
    {

         result = database.Query<alphabets>("select * from Tbl_Alphabets where Alpabet_ID =" + ID + "-1").LastOrDefault();
        result = new alphabets();
       String val = result.English_Alphabet;
        ID = result.Alpabet_ID;
    }


    public class alphabets
    {
        public string Lang_ID { get; set; }
        public int Alpabet_ID { get; set; }
        public string English_Alphabet { get; set; }
        public string Language_Alphabet { get; set; }
        public string Sound_Default_Path { get; set; }
        public string Sound_lang_Path { get; set; }
        public alphabets()
        {
        }
    }
    void OnStart(object sender, EventArgs e)
    {
        GetAlphabets();
        val = result.English_Alphabet;
        English.Text = val;

        DependencyService.Get<ITextToSpeech>().speak(English.Text + " " + "" + Hausa.Text);

    }

    void OnPrevious(object sender, EventArgs e)
    {
        GetPrevious();
        val = result.English_Alphabet;
        English.Text = val;

    }

    void OnNext(object sender, EventArgs e)
    {
        GetNext();
        English.Text = val;
    }
}

}

Answers

  • GidimooGidimoo USMember

    I use the same code in windows phone 8.1 and it works just fine but not working in cross-platform and not giving me any error. but i know that i am missing something somewhere, please help out.

  • shash12shash12 INMember ✭✭

    @Gidimoo database.Query("select * from Tbl_Alphabets where Alpabet_ID = 1").FirstOrDefault(); will return List of Collection of alphabets and here result = new alphabets(); you are creating instance of your model class it and storing it in result which will throw error.

  • GidimooGidimoo USMember

    Please what should i do then.

  • GidimooGidimoo USMember

    whenever i remove the new instance of the model the code throws null reference Exception.

Sign In or Register to comment.