Question

I have a WP7 app that uses a SQLite database. A feature of the app is backing up to SkyDrive, which works fine, but afterwards my app throws the "unable to open database file." If I wait a bit (anywhere from 10 to 60 seconds) it'll start working again. I don't know if the problem is with the Live SDK, IsolatedStorage, or something else, but it seems to me to be a problem with the file still being in use after the upload is complete. (For what it's worth, I don't believe it to be strictly a SQLite error.)

I'm using the following code (edited down for brevity):

private void Upload() {
    this._LiveClient.UploadProgressChanged += UploadProgressChanged;
    this._LiveClient.UploadCompleted += UploadCompleted;

    IsolatedStorageFileStream stream = null;

    try {
        using( IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication() ) {
            stream = store.OpenFile( "mydatabasefile.dat", FileMode.Open, FileAccess.Read );
        }

        this._LiveClient.UploadAsync( "DestinationPath", "DestinationFilename.sqlite", true, stream, null );
    } catch( Exception e ) {
        MessageBox.Show( e.Message, "Error", MessageBoxButton.OK );
    }
}

private void UploadCompleted( object sender, LiveOperationCompletedEventArgs e ) {
    if( e.Error == null ) {
        MessageBox.Show( "File successfully uploaded.", "Backup", MessageBoxButton.OK );
        if( this.NavigationService.CanGoBack ) this.NavigationService.GoBack();
    } else {
        MessageBox.Show( e.Error.Message, "Upload Error", MessageBoxButton.OK );
    }
}

After navigating back (which will always be my main page), anything I try to do that requires database access (such as navigating to my view page and displaying a record) throws the error (again, until I wait a bit).

I'm not sure if I need a Close statement somewhere, or even just a loop checking the state of the file until it's closed, or what.

Was it helpful?

Solution

The thing you need to do is add stream.Dispose(); in UploadCompleted and that should fix you problem (the only thing to keep in mind is that you will not be able to access the database during the upload, if you need to do that also then you will need to create a copy of the Db file before uploading it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top