0

i have recently trying to create database related windows phone 8 application because i want update content dynamically and whenever i updated it will automatically updated in my application also.,

so that i have decided to keep all my data in db (.sqlite) file and upload it on server

Note that i have not created any Database in local and i will try to download sqlite file from server ., from that sqlite file i ll get database tables.,

so my Requirement is

1) how to download (.sqlite) file from the server via server URL like (www.example.com/folder/sample.sqlite)

2) how to save that sqlite file in local or isolated storage

i have searched and got some samplew from online., but all of this ll create database in local itself.,

so please someone give me solution that how to download .sqlite file directly from URL and save it in local.,

Punniyam Moorthi
  • 893
  • 1
  • 11
  • 19
  • 1
    If you want to download a file, then I've written [an answer here](http://stackoverflow.com/a/21811036/2681948). Method first is not suitable, but second and third may be. You can also think about Background Transfers – Romasz Feb 17 '14 at 12:05
  • i tried 3 rd mwthod from the link which u given., but i got error like this " Inconsistent accessibility: return type 'System.Threading.Tasks.Task' is less accessible than method 'iccWorldT20i.MainPage.DownloadFileSimle(System.Uri, string)' " ., please help me to solve., or give some other examples – Punniyam Moorthi Feb 18 '14 at 08:30
  • 1
    Try to make enum DownloadStatus public (I've edited answer). Give me a sign it that helped. – Romasz Feb 18 '14 at 08:49
  • thanks for your reply., but when i try to download i got new error like " A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.ni.dll " – Punniyam Moorthi Feb 18 '14 at 11:02
  • Is it possicle that you share the project (a simple example that reproduce the exception)? It will be much easier to help you. And what are the details of the exception? – Romasz Feb 18 '14 at 11:07
  • sorry for delay., my project file is [here](https://www.dropbox.com/s/g1mxdywdblpl1am/iccWorldT20i.rar). and sample sqlite file is [here](https://dl.dropboxusercontent.com/s/nz9107khswqttyp/sample.sqlite?dl=1&token_hash=AAE7EOhKzpVlAbCUlgwToURZOg0xZzMesu_gPTcLceZzDg)., sorry this is the first time i shared and please try solve my problem., thank you for spending your time., – Punniyam Moorthi Feb 18 '14 at 12:10
  • Now you can delete those files and comment. – Romasz Feb 18 '14 at 12:50
  • thank you so much., really it works., again one request that is there any best link to do download process in Background Transfers. because you already said like this is foreground operation and background transfer is best way for app., is there any link that can easily understand Background Transfers., – Punniyam Moorthi Feb 18 '14 at 13:32
  • You have always [MSDN](http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202955%28v=vs.105%29.aspx) and try to google it, probably there are blogs and tutorials. Be aware that Background Transfers have limits (see at MSDN) and not in every case may be better. – Romasz Feb 18 '14 at 13:36
  • thank you thousand times for spent your time for me ., i ll see u with another question soon., also thanks a lot to stack overflow.. – Punniyam Moorthi Feb 18 '14 at 13:48

1 Answers1

1

Ok now I understand. The problem is that the link doesn't contain the file directly you will have to use GetResponse() first and then get ResponseStream.

To make it work you have to extend your WebClient (again):

public static class Extensions
{
    public static Task<WebResponse> GetResponseAsync(this WebRequest webRequest)
    {
        TaskCompletionSource<WebResponse> taskComplete = new TaskCompletionSource<WebResponse>();
        webRequest.BeginGetResponse(
            asyncResponse =>
            {
                try
                {
                    WebRequest responseRequest = (WebRequest)asyncResponse.AsyncState;
                    WebResponse someResponse = (WebResponse)responseRequest.EndGetResponse(asyncResponse);
                    taskComplete.TrySetResult(someResponse);
                }
                catch (WebException webExc)
                {
                    WebResponse failedResponse = (WebResponse)webExc.Response;
                    taskComplete.TrySetResult(failedResponse);
                }
                catch (Exception exc) { taskComplete.SetException(exc); }
            }, webRequest);
        return taskComplete.Task;
    }

After that you only have to put a little modification to your code:

public static async Task<DownloadStatus> DownloadFileSimle(Uri fileAdress, string fileName)
{
    try
    {
       WebRequest request = WebRequest.Create(fileAdress);
       if (request != null)
       {
           WebResponse response2 = await request.GetResponseAsync();
           using (Stream resopnse = response2.GetResponseStream())
           {
           // the rest goes the same

As I have tried it works and the file is downloaded with its content.

Hope this helps.

Romasz
  • 29,662
  • 13
  • 79
  • 154