4

I have created a small Desktop [WinForm] data insertion application using C# VS2013 and SQLite database. It is working Fine and all the CURD Operation. But when I created Setup of this application using Advance Installer. Then whenever i run the application and try to Insert Data it popup this screen

This is The Error I Got.

And Thanks..

Here is Some Code Snipt.

//Add Property Function.
string ConnectionString = "Data Source=database/MyProperty.db;Version=3;Read Only=False";

    public static long AddPropertyToDatabae( Property property )
    {
        SQLiteConnection con = new SQLiteConnection( ConnectionString );
        SQLiteCommand cmd = new SQLiteCommand
        {
            Connection = con,
            CommandText =
                "INSERT INTO Properties (PropertyName,PropertyAddress,PropertyCity,PropertyState,PropertyZip,PropertyNotes)" +
                " values (@Name,@Address,@City,@State,@Zip,@Notes)"
        };
        cmd.Parameters.AddWithValue( "@Name", property.PropertyName );
        cmd.Parameters.AddWithValue( "@Address", property.PropertyAddress );
        cmd.Parameters.AddWithValue( "@City", property.PropertyCity );
        cmd.Parameters.AddWithValue( "@State", property.PropertyState );
        cmd.Parameters.AddWithValue( "@Zip", property.PropertyZip );
        cmd.Parameters.AddWithValue( "@Notes", property.PropertyNotes );

        con.Open();
        cmd.ExecuteNonQuery();

        // Get the Last Inserted RowId.
        cmd.CommandText = "select last_insert_rowid()";
        long rowid = ( long )cmd.ExecuteScalar();

        con.Close();
        return rowid;
    }

Note

This is 100% workring code on the VS. but it only generates error when i Create Setup of the Application

18
  • inside your setup application are you doing any Create commands.. if this is an existing SqlLite file perhaps there is an issue with how you are trying to deploy it.. can you show use code.. also can you step through the installer code.. to see if you can pinpoint and or recreate the error..? Commented Oct 10, 2015 at 16:29
  • No. I am only using INSERT and SELECT Commands.. Commented Oct 10, 2015 at 16:32
  • there must be something that you are doing in regards to the insert or Select Command can you show some of your code for those two CRUD Functions...? Commented Oct 10, 2015 at 16:44
  • I will show you in a mint... Commented Oct 10, 2015 at 16:48
  • I have added the Code Snippet to Question You can check.. Commented Oct 10, 2015 at 17:02

4 Answers 4

2

Technically error was not in the SQLite Database. But it was the Folder Error. I was Creating the Sub folder For the database file and it does not has the permission as the Local Output folder. So I just moved my database to Main BIN folder it Worked.:-)

Here is the Link where i found the solution. SQLite for Windows Runtime is returning an "ReadOnly" error SQLiteException object

-- Important --

If you got problem like that please try to change the directory of your database file to main output directory..

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

4 Comments

I had the same underlying issue, but instead of moving the database file to a different folder, I was also able to get around it by running the executable that's trying to write to the database as Administrator
@JoeIrby you are right. I found this out later and now every time I have to use the DB like this. I always make the exe run as Administrator so the DB file has the proper read and write access.
@Awais can you please provide the new connection string
@priyankas you can do two things, 1: make your application Run as Administrator (Administrator Access) then you can put your db file anywhere in the project directory and it will worked perfectly. 2: just put the db file to BIN folder and use the connection string like this " Data Source=YourDatabase.db;Version=3; ".
1

During creating windows installer setup file : set Application folder path: [WindowsVolume][ProductName]

I used Advanced Installer to create windows installer setup file.

enter image description here

If you try to set up path [ProgramFilesFolder][Manufacturer][ProductName] then you are not able to write data in Sqlite but if you run the application in Run As Administrator mode then Sqlite readonly issue will be solved.

Comments

0

Change your Sql Command to the following so that you do not have to run 2 separate sql commands there is no guarantee that you will get the correct last inserted row if 2 people hit that code and insert at the same time. I would need to see the installer code to see what could be happening there..

"INSERT INTO Properties (PropertyName,PropertyAddress,PropertyCity,PropertyState,PropertyZip,PropertyNotes)" +
  " values (@Name,@Address,@City,@State,@Zip,@Notes)SELECT CAST(scope_identity() AS int)";
rowid = (int)cmd.ExecuteScalar();   

Comments

0

(Maybe a little late...)

I just solved this problem.

An app can read and write its LocalFolder arbitrarily, so you just need to copy your data file to that folder.

The code is like:

StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("your-file-path"));
await file.CopyAsync(localFolder, "any name", NameCollisionOption.FailIfExists);

Then you can create SQLiteConnection in this way:

string dbpath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "abc.db");
SQLiteConnection connection = new SQLiteConnection($"Data Source={dbpath}")

Comments

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.