code works in Windows but not in Android , Do you see any issue with Sqllite nugets

Maui Learner 440 Reputation points
2024-03-23T13:03:58.1433333+00:00
@page "/"
@using System.Text;
@using System.Net.Http.Headers;
@using Microsoft.AspNetCore.Components;
@using Newtonsoft.Json;
@using RateApp.Models;
@if (listcount > 0)
{
 
<div class="row">
      
    <div class="col-md-3">
        <p>Deposit Amount INR</p>
    </div>
 
<input type="range" min="1000" max="100000" step="1000" @bind="@amount"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" />
<p>@amount</p>
</div>
<br />
<div class="row">
  <p>Years:</p>
<input type="range" min="1" max="5" step="1" @bind="@years"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" />
</div>
<p>@years</p>
<br />
<div class="row">
    <select @bind="@srcitizen"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" >
    <option value="0">Non Sr Citizen</option>
        <option value="1">Sr Citizen</option>
        </select>
</div>
@if (srcitizen > 0)
{
      <p>Sr Citizen</p>
}
@if (srcitizen == 0)
{
    <p>Non Sr Citizen</p>
}
<br />
<div class="row">
    <select @bind="@cumulative"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" >
        <option value="0">Interest Non Cumulative</option>
        <option value="1">Interest Cumulated Quarterly</option>
            <option value="2">Interest Cumulated Semi annually</option>
            
            <option value="3">Interest Cumulated Annually</option>
    </select>
</div>
@if (cumulative == 0)
{
     <p>Interest Non Cumulative</p>
}
@if (cumulative == 1)
{
    <p>Interest Cumulated Quarterly</p>
}
@if (cumulative == 2)
{
    <p>Interest Cumulated Semi annual</p>
}
    @if (cumulative == 3)
    {
        <p>Interest Cumulated Annual</p>
    }
<div class="row">
    <div class="col-md-3">
        <p>Deposit Amount INR</p>
    </div>
    <p>@amount</p>
</div>
<br />
<div class="row">
    <p>Years:</p>
    <p>@years</p>
</div>
<br />
<div class="row">
    <p>Interest:</p>
    <p>@dispint</p>
</div>
 
<div>
    <div class="col-md-3">
            <p>Maturity Amount INR </p>
    </div>
    <div>@final</div>
       
        
</div>
}
@if(listcount==0)
{
    <p>Loading... </p>
}
 
@code {
    QuestionsDatabase questionsDatabase;
    public int srcitizen;
    public int listcount; public int totfinal;
    public string codex; public string  jsonstring ; public bool statcode;
    List<InQuestion> Questions = new List<InQuestion>();
    public int number; int years = 1; decimal interest; decimal srinterest; int totalquestions; int maxnumber; int amount = 1000; decimal imint; decimal final; decimal finalcompound; decimal dispint;
    decimal imstint; decimal srfinal; decimal srfinalcompound; decimal cumulative;
    protected override async Task OnInitializedAsync()
    {
        string connecteddb = "N";
        string jwturl = "https://quizapijwt.azurewebsites.net/api/";
        using (var client1 = new HttpClient())
        {
            questionsDatabase = new QuestionsDatabase();
            client1.BaseAddress = new Uri(jwturl);
            client1.DefaultRequestHeaders.Clear();
            client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = null;
            try
            { 
                response = await client1.GetAsync("RateCards");
            }
            catch (Exception ex)
            {
                Questions = await questionsDatabase.GetItemsAsync();
                Questions = Questions.OrderBy(x => x.DaysBegin).ToList();
                listcount = Questions.Count();
                var imint = (from l in Questions
                             where l.DaysBegin == years
                             select l.Interest).FirstOrDefault();
                decimal amt = amount;
                decimal finint = Decimal.Multiply(amt, imint);
                interest = Decimal.Multiply(finint, years);
                final = Math.Round(((interest / 100) + amt), 2);
                double citotal; int t = 0;
                double ciint; decimal cifinal = 0.0M;
                ciint = (double)imint;
                int annualCompound = 200;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                finalcompound = Math.Round(cifinal, 2);
                var srimint = (from l in Questions
                               where l.DaysBegin == years
                               select l.SrInterest).FirstOrDefault();
                //
                decimal srfinint = Decimal.Multiply(amt, srimint);
                srinterest = Decimal.Multiply(srfinint, years);
                srfinal = Math.Round(((srinterest / 100) + amt), 2);
                //
                double srciint; decimal srcifinal = 0.0M;
                srciint = (double)srimint;
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
                srfinalcompound = Math.Round(srcifinal, 2);
                dispint = imint; connecteddb = "Y";
            }
            if (connecteddb == "N")
            {
                if (response.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    List<InQuestion> itemInTheDB = await questionsDatabase.GetItemsAsync();
                    foreach (InQuestion item in itemInTheDB)
                    {
                        var result = Questions.FirstOrDefault(c => c.No == item.No);
                        if (result == null)
                        {
                            await questionsDatabase.DeleteItemAsync(item);
                        }
                    }
                    try
                    {
                        jsonstring = await response.Content.ReadAsStringAsync();
                        Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonstring);
                        //       connected = "Y";
                        foreach (InQuestion itemdb in Questions)
                        {
                            await questionsDatabase.SaveItemAsync(itemdb);
                        }
                        Questions = Questions.OrderBy(x => x.DaysBegin).ToList();
                        listcount = Questions.Count();
                        var imint = (from l in Questions
                                     where l.DaysBegin == years
                                     select l.Interest).FirstOrDefault();
                        decimal amt = amount;
                        decimal finint = Decimal.Multiply(amt, imint);
                        interest = Decimal.Multiply(finint, years);
                        final = Math.Round(((interest / 100) + amt), 2);
                        double citotal; int t = 0;
                        double ciint; decimal cifinal = 0.0M;
                        ciint = (double)imint;
                        int annualCompound = 200;
                        cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                        finalcompound = Math.Round(cifinal, 2);
                        var srimint = (from l in Questions
                                       where l.DaysBegin == years
                                       select l.SrInterest).FirstOrDefault();
                        //
                        decimal srfinint = Decimal.Multiply(amt, srimint);
                        srinterest = Decimal.Multiply(srfinint, years);
                        srfinal = Math.Round(((srinterest / 100) + amt), 2);
                        //
                        double srciint; decimal srcifinal = 0.0M;
                        srciint = (double)srimint;
                        srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
                        srfinalcompound = Math.Round(srcifinal, 2);
                        dispint = imint;
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
    }
    private void UpdateCurrentInterestValue()
    {
        try
        { 
            var    imint  =  (from l in Questions
                         where l.DaysBegin == years
                         select l.Interest).FirstOrDefault() ; 
            var srimint = (from l in Questions
                           where l.DaysBegin == years
                           select l.SrInterest).FirstOrDefault();
            decimal amt = amount;
            if (cumulative == 0)
            {
                decimal finint = Decimal.Multiply(amt, imint);
                interest = Decimal.Multiply(finint, years);
                final =  Math.Round(((interest / 100) + amt),2);
                decimal srfinint = Decimal.Multiply(amt, srimint);
                srinterest = Decimal.Multiply(srfinint, years);
                srfinal =  Math.Round(((srinterest / 100) + amt),2);
            }
            double ciint; double srciint; decimal cifinal = 0.0M;
            decimal srcifinal = 0.0M;
            if (cumulative == 3)
            {
                int annualCompound = 100;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (1 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (1 * years)));
            }
          
            if (cumulative == 2)
            {
                int annualCompound = 200;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
            }
            if (cumulative == 1)
            {
                int annualCompound = 400;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (4 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (4 * years)));
            }
            if (cumulative > 0)
            {
                finalcompound =  Math.Round(cifinal,2);
                final = finalcompound;
            }
            srfinalcompound =  Math.Round(srcifinal,2);
            dispint = imint;
            if (srcitizen > 0)
            {
                if (cumulative == 0)
                { final = srfinal; }
                else
                { final = srfinalcompound; }
                dispint = srimint;
            }
        }
        catch (Exception ex)
        {
              
        }
    }
}
 
 @page "/"
@using System.Text;
@using System.Net.Http.Headers;
@using Microsoft.AspNetCore.Components;
@using Newtonsoft.Json;
@using RateApp.Models;
@if (listcount > 0)
{
 
<div class="row">
      
    <div class="col-md-3">
        <p>Deposit Amount INR</p>
    </div>
 
<input type="range" min="1000" max="100000" step="1000" @bind="@amount"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" />
<p>@amount</p>
</div>
<br />
<div class="row">
  <p>Years:</p>
<input type="range" min="1" max="5" step="1" @bind="@years"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" />
</div>
<p>@years</p>
<br />
<div class="row">
    <select @bind="@srcitizen"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" >
    <option value="0">Non Sr Citizen</option>
        <option value="1">Sr Citizen</option>
        </select>
</div>
@if (srcitizen > 0)
{
      <p>Sr Citizen</p>
}
@if (srcitizen == 0)
{
    <p>Non Sr Citizen</p>
}
<br />
<div class="row">
    <select @bind="@cumulative"
    @onchange="@UpdateCurrentInterestValue" @bind:event="oninput" >
        <option value="0">Interest Non Cumulative</option>
        <option value="1">Interest Cumulated Quarterly</option>
            <option value="2">Interest Cumulated Semi annually</option>
            
            <option value="3">Interest Cumulated Annually</option>
    </select>
</div>
@if (cumulative == 0)
{
     <p>Interest Non Cumulative</p>
}
@if (cumulative == 1)
{
    <p>Interest Cumulated Quarterly</p>
}
@if (cumulative == 2)
{
    <p>Interest Cumulated Semi annual</p>
}
    @if (cumulative == 3)
    {
        <p>Interest Cumulated Annual</p>
    }
<div class="row">
    <div class="col-md-3">
        <p>Deposit Amount INR</p>
    </div>
    <p>@amount</p>
</div>
<br />
<div class="row">
    <p>Years:</p>
    <p>@years</p>
</div>
<br />
<div class="row">
    <p>Interest:</p>
    <p>@dispint</p>
</div>
 
<div>
    <div class="col-md-3">
            <p>Maturity Amount INR </p>
    </div>
    <div>@final</div>
       
        
</div>
}
@if(listcount==0)
{
    <p>Loading... </p>
}
 
@code {
    QuestionsDatabase questionsDatabase;
    public int srcitizen;
    public int listcount; public int totfinal;
    public string codex; public string  jsonstring ; public bool statcode;
    List<InQuestion> Questions = new List<InQuestion>();
    public int number; int years = 1; decimal interest; decimal srinterest; int totalquestions; int maxnumber; int amount = 1000; decimal imint; decimal final; decimal finalcompound; decimal dispint;
    decimal imstint; decimal srfinal; decimal srfinalcompound; decimal cumulative;
    protected override async Task OnInitializedAsync()
    {
        string connecteddb = "N";
        string jwturl = "https://quizapijwt.azurewebsites.net/api/";
        using (var client1 = new HttpClient())
        {
            questionsDatabase = new QuestionsDatabase();
            client1.BaseAddress = new Uri(jwturl);
            client1.DefaultRequestHeaders.Clear();
            client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = null;
            try
            { 
                response = await client1.GetAsync("RateCards");
            }
            catch (Exception ex)
            {
                Questions = await questionsDatabase.GetItemsAsync();
                Questions = Questions.OrderBy(x => x.DaysBegin).ToList();
                listcount = Questions.Count();
                var imint = (from l in Questions
                             where l.DaysBegin == years
                             select l.Interest).FirstOrDefault();
                decimal amt = amount;
                decimal finint = Decimal.Multiply(amt, imint);
                interest = Decimal.Multiply(finint, years);
                final = Math.Round(((interest / 100) + amt), 2);
                double citotal; int t = 0;
                double ciint; decimal cifinal = 0.0M;
                ciint = (double)imint;
                int annualCompound = 200;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                finalcompound = Math.Round(cifinal, 2);
                var srimint = (from l in Questions
                               where l.DaysBegin == years
                               select l.SrInterest).FirstOrDefault();
                //
                decimal srfinint = Decimal.Multiply(amt, srimint);
                srinterest = Decimal.Multiply(srfinint, years);
                srfinal = Math.Round(((srinterest / 100) + amt), 2);
                //
                double srciint; decimal srcifinal = 0.0M;
                srciint = (double)srimint;
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
                srfinalcompound = Math.Round(srcifinal, 2);
                dispint = imint; connecteddb = "Y";
            }
            if (connecteddb == "N")
            {
                if (response.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    List<InQuestion> itemInTheDB = await questionsDatabase.GetItemsAsync();
                    foreach (InQuestion item in itemInTheDB)
                    {
                        var result = Questions.FirstOrDefault(c => c.No == item.No);
                        if (result == null)
                        {
                            await questionsDatabase.DeleteItemAsync(item);
                        }
                    }
                    try
                    {
                        jsonstring = await response.Content.ReadAsStringAsync();
                        Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonstring);
                        //       connected = "Y";
                        foreach (InQuestion itemdb in Questions)
                        {
                            await questionsDatabase.SaveItemAsync(itemdb);
                        }
                        Questions = Questions.OrderBy(x => x.DaysBegin).ToList();
                        listcount = Questions.Count();
                        var imint = (from l in Questions
                                     where l.DaysBegin == years
                                     select l.Interest).FirstOrDefault();
                        decimal amt = amount;
                        decimal finint = Decimal.Multiply(amt, imint);
                        interest = Decimal.Multiply(finint, years);
                        final = Math.Round(((interest / 100) + amt), 2);
                        double citotal; int t = 0;
                        double ciint; decimal cifinal = 0.0M;
                        ciint = (double)imint;
                        int annualCompound = 200;
                        cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                        finalcompound = Math.Round(cifinal, 2);
                        var srimint = (from l in Questions
                                       where l.DaysBegin == years
                                       select l.SrInterest).FirstOrDefault();
                        //
                        decimal srfinint = Decimal.Multiply(amt, srimint);
                        srinterest = Decimal.Multiply(srfinint, years);
                        srfinal = Math.Round(((srinterest / 100) + amt), 2);
                        //
                        double srciint; decimal srcifinal = 0.0M;
                        srciint = (double)srimint;
                        srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
                        srfinalcompound = Math.Round(srcifinal, 2);
                        dispint = imint;
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
    }
    private void UpdateCurrentInterestValue()
    {
        try
        { 
            var    imint  =  (from l in Questions
                         where l.DaysBegin == years
                         select l.Interest).FirstOrDefault() ; 
            var srimint = (from l in Questions
                           where l.DaysBegin == years
                           select l.SrInterest).FirstOrDefault();
            decimal amt = amount;
            if (cumulative == 0)
            {
                decimal finint = Decimal.Multiply(amt, imint);
                interest = Decimal.Multiply(finint, years);
                final =  Math.Round(((interest / 100) + amt),2);
                decimal srfinint = Decimal.Multiply(amt, srimint);
                srinterest = Decimal.Multiply(srfinint, years);
                srfinal =  Math.Round(((srinterest / 100) + amt),2);
            }
            double ciint; double srciint; decimal cifinal = 0.0M;
            decimal srcifinal = 0.0M;
            if (cumulative == 3)
            {
                int annualCompound = 100;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (1 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (1 * years)));
            }
          
            if (cumulative == 2)
            {
                int annualCompound = 200;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (2 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (2 * years)));
            }
            if (cumulative == 1)
            {
                int annualCompound = 400;
                //   int t = 0;
                ciint = (double)imint;
                srciint = (double)srimint;
                cifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + ciint / annualCompound), (4 * years)));
                srcifinal = Decimal.Multiply(amt, (decimal)Math.Pow((1 + srciint / annualCompound), (4 * years)));
            }
            if (cumulative > 0)
            {
                finalcompound =  Math.Round(cifinal,2);
                final = finalcompound;
            }
            srfinalcompound =  Math.Round(srcifinal,2);
            dispint = imint;
            if (srcitizen > 0)
            {
                if (cumulative == 0)
                { final = srfinal; }
                else
                { final = srfinalcompound; }
                dispint = srimint;
            }
        }
        catch (Exception ex)
        {
              
        }
    }
}
 
  public static class Constants
    {
        public const string DatabaseFilename = "MCQs.db3";
        public const SQLite.SQLiteOpenFlags Flags =
            // open the database in read/write mode
            SQLite.SQLiteOpenFlags.ReadWrite |
            // create the database if it doesn't exist
            SQLite.SQLiteOpenFlags.Create |
            // enable multi-threaded database access
            SQLite.SQLiteOpenFlags.SharedCache;
        public static string DatabasePath =>
          Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
    }
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RateApp.Models
{
   public class InQuestion
    {
        [PrimaryKey, Indexed]
        public int No { get; set; }
            public int DaysBegin { get; set; }
            public int DaysEnd { get; set; }
            public decimal Interest { get; set; }
            public decimal SrInterest { get; set; }
       
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RateApp.Models
{
   
        internal class PostData
        {
            public string Name { get; set; }
            public string Password { get; set; }
        }
  
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
using RateApp.Models;
namespace RateApp.Models
{
    
 
        public class QuestionsDatabase
        {
            SQLiteAsyncConnection Database;
            private SQLiteAsyncConnection _dbConnection;
            public const string DatabaseFilename = "MCQ.db3";
            async Task Init()
            {
                string dbPath =
                 Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
                FileStream fileStream = new FileStream(dbPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                _dbConnection = new SQLiteAsyncConnection(dbPath);
                if (Database is not null)
                    return;
                Database = new SQLiteAsyncConnection(dbPath, Constants.Flags);
                var result = await Database.CreateTableAsync<InQuestion>();
            }
            public async Task<List<InQuestion>> GetItemsAsync()
            {
                await Init();
                var res = await Database.Table<InQuestion>().ToListAsync();
                return res;
            }
            public async Task<InQuestion> GetItemAsync(int id)
            {
                await Init();
                return await Database.Table<InQuestion>().Where(i => i.No == id).FirstOrDefaultAsync();
            }
            public async Task<int> SaveItemAsync(InQuestion item)
            {
                await Init();
                InQuestion res = await GetItemAsync(item.No);
                if (res != null)
                    return await Database.UpdateAsync(item);
                else
                    return await Database.InsertAsync(item);
            }
            public async Task<int> DeleteItemAsync(InQuestion item)
            {
                await Init();
                return await Database.DeleteAsync(item);
            }
        }
    }
 


The above code works in Windows but not in Android , Do you see any issue with Sqllite nugets please suggest.It is not connecting to Sqlite from Android.

Capturesqllite.JPG

Added this line in MauiProgram.cs

builder.Services.AddSingleton<QuestionsDatabase>();

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,895 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Maui Learner 440 Reputation points
    2024-04-07T12:29:40.2833333+00:00

    Yonglun Liu (Shanghai Wicresoft Co,.Ltd.)

    I created a fresh app and installed nuget it works now.

    0 comments No comments