locked
Populate a local SQLite Database Using Web App RRS feed

  • Question

  • User-1314346660 posted

    Hello Experts,

    I have a web application that needs to push data to an SQLite db that is on the user machine. It is quite straight forward. It takes data from an Azure SQL db and pushes to the local SQLite db.

    The SQLite db is here:

    string ConnectionStringDestination = @"Data Source=C:\TestSQLite\Testdb.db;Version=3;";

    And here is my code to push:

    protected void Button1_Click(object sender, EventArgs e)
    {
    string connString = @"My Conn";
    string ConnectionStringDestination = @"Data Source=C:\TestSQLite\Testdb.db;Version=3;";

    var tblBatchNumbers = new DataTable();
    using (var da = new SqlDataAdapter("exec spBatchNumbers", connString))
    //using (var da = new SqlDataAdapter("SELECT BatchNumber,Programme FROM BatchNumbers", connString))
    {
    da.Fill(tblBatchNumbers);
    }
    SQLiteConnection cons = new SQLiteConnection(ConnectionStringDestination,true);

    cons.Open();
    SQLiteCommand Command = new SQLiteCommand("DELETE FROM BatchNumbers", cons);
    Command.ExecuteNonQuery();
    cons.Close();
    using (SQLiteConnection con = new SQLiteConnection(ConnectionStringDestination,true))
    {
    con.Open();

    using (SQLiteTransaction transaction = con.BeginTransaction())
    {
    foreach (DataRow row in tblBatchNumbers.Rows)
    {
    using (SQLiteCommand sqlitecommand = new SQLiteCommand("INSERT INTO BatchNumbers(RecordId,BatchNumber,ExpiryDate,Programme,Route,Dose) VALUES ('"
    + Convert.ToString(row[0]) + "','"
    + Convert.ToString(row[1]) + "','"
    + Convert.ToString(row[2]) + "','"
    + Convert.ToString(row[3]) + "','"
    + Convert.ToString(row[4]) + "','"
    + Convert.ToString(row[5]) + "')", con))
    {
    sqlitecommand.ExecuteNonQuery();
    }
    }
    transaction.Commit();
    }
    }
    }

    It all works brilliantly when running in Local Host. However, once I deploy the app to Azure then the process fails. Here is the error:


    [SQLiteException (0xe): unable to open database file]
    System.Data.SQLite.SQLite3.Open(String strFilename, String vfsName, SQLiteConnectionFlags connectionFlags, SQLiteOpenFlagsEnum openFlags, Int32 maxPoolSize, Boolean usePool) +644
    System.Data.SQLite.SQLiteConnection.Open() +5579
    HTML5OfflineApplication.BulkCopy_DataTableToSQLLite.Button1_Click(Object sender, EventArgs e) in C:\Users\Me\OneDrive - Me\Visual Studio\source\repos\Offline\Offline\Secure\BulkCopy_DataTableToSQLLite.aspx.cs:32
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +109
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +108
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +31
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3470

    I think that the error is caused by the application not being able to reference/find the database. The file and folder has WRITE access.

    Any ideas. I have drawn a blank on this now for 48hrs!!!!!!

    Thanks very much always.

    Billson3000

    Tuesday, June 4, 2019 9:01 AM

Answers

All replies

  • User475983607 posted

    If I understand the problem correctly, you want to update a SQLite DB on a user's machine from a remote web application running on Azure.  If so, the approach is not possible because the Azure web application cannot make requests to the user's machine.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 10:13 AM
  • User-1314346660 posted

    Oh dear. That's a bit disappointing!!

    Is there a work around?

    Could I build a windows app that could call the Azure databases instead?

    Tuesday, June 4, 2019 10:18 AM
  • User-821857111 posted

    Could I build a windows app that could call the Azure databases instead?
    Yes. Your only option is to build an application that makes remote calls from the users' machine.

    Further discussion on desktop application development is off topic here.

    Tuesday, June 4, 2019 10:20 AM
  • User-1314346660 posted

    Cheers Mike.

    I have only developed web apps and aspnet.

    Can you recommend a starting tutorial or learning space?

    Tuesday, June 4, 2019 10:26 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 10:46 AM
  • User475983607 posted

    I have only developed web apps and aspnet.

    Can you recommend a starting tutorial or learning space?

    The "Docs" link above has links to all the different types of .NET projects you can build.

    https://docs.microsoft.com/en-us/dotnet/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 10:50 AM
  • User-1314346660 posted

    Thanks very much Mike and MGE.

    Oh dear. Its a shame I cant do what I wanted! As an aside, why is it that a web app cannot access the local machine? Is this a security issue?

    Tuesday, June 4, 2019 11:46 AM
  • User475983607 posted

    Oh dear. Its a shame I cant do what I wanted! As an aside, why is it that a web app cannot access the local machine? Is this a security issue?

    It's s TCP/IP fundamental.  It's possible with some networking (the approach depends on your network topology) and crafting a listener on the client machine.  Being that you have to write/install software on the client for either approach it easy to build a client side application.

    Tuesday, June 4, 2019 12:32 PM
  • User-1314346660 posted

    Thanks a lot MGE.

    Back to the drawing board with a separate client side application with a local storage solution.

    Thanks again. I appreciate your time and help!

    Tuesday, June 4, 2019 3:04 PM
  • User61956409 posted

    Hi Billson3000,

    takes data from an Azure SQL db and pushes to the local SQLite db

    Firstly, glad to hear that you get a solution with the help of mike and mgebhard.

    Besides, based on your code and description, it seems that you'd like to query data from Azure SQL db and push the retrieved data to your local SQLite db after user click the button from your application, to achieve the requirement, here is another possible solution:

    1) Create and insert a new message in queue in button click event. And if possible, you can use [Azure Queue service](https://docs.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues)

    2) Create and run QueueTrigger [Azure WebJobs](https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to) on user's local to consume queue messages

    3) In QueueTrigger Azure WebJobs code logic, you can query data from Azure SQL db and push the retrieved data to local SQLite db as you did before.

    With Regards,

    Fei Han

    Wednesday, June 5, 2019 2:55 AM