1

I want to develop an app in windows phone 8. I am totally new to this. I want to create a database for that app from which I can perform CRUID Operations. I found some information while browsing and watching videos but I did't understand much of it.

Some Steps I did:

  1. Installed windows phone app 8 sdk for vs2012
  2. Added some Sqlite extension from Manage Nuget Packages.
  3. Developed a basic interface for the app.
  4. Copied and pasted the code with few changes

What I want:

  1. Permanently Insert and Fetch data from database (I had downloaded a code from some website but after running it when I close the emulator and try to view the data previously entered, it won't return it)
  2. Like it should be stored in phone memory or any such place

  3. Display the fetched data in listview or grid

Please send me the link that i can go through or any such resembling question asked here

The MainPage.xaml.cs Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using CustomerPhoneApp.Resources;
using SQLite;
using System.IO;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Popups;
using System.Data.Linq;
using System.Diagnostics;

namespace CustomerPhoneApp
{
public partial class MainPage : PhoneApplicationPage
{
    [Table("Users")]
    public class User
    {
        [PrimaryKey, Unique]

        public string Name { get; set; }
        public string Age { get; set; }
    }

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        try
        {
            var path = ApplicationData.Current.LocalFolder.Path + @"\users.db";
            var db = new SQLiteAsyncConnection(path);
            await db.CreateTableAsync<User>();
        }
        catch (Exception)
        {
        }
    }

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        if (txtName.Text != "" && txtAge.Text != "")
        {
            var path = ApplicationData.Current.LocalFolder.Path + @"\users.db";
            var db = new SQLiteAsyncConnection(path);
            var data = new User
            {
                Name = txtName.Text,
                Age = txtAge.Text,
            };

            int x = await db.InsertAsync(data);
        }
        else
        {
            MessageBox.Show("enter the title and Notes");
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    { 
        RetriveUserSavedData();
    }

    private async void RetriveUserSavedData()
    {
        string Result = "";
        var path = ApplicationData.Current.LocalFolder.Path + @"\users.db";
        var db = new SQLiteAsyncConnection(path);

        List<User> allUsers = await db.QueryAsync<User>("Select * From Users");
        var count = allUsers.Any() ? allUsers.Count : 0;

        foreach (var item in allUsers)
        {
            Result += "Name: " + item.Name + "\nAge: " + item.Age.ToString() + "\n\n";
        }

        if (Result.ToString() == "")
        {
            MessageBox.Show("No Data");
        }

        else
        {
            MessageBox.Show(Result.ToString());
        }
    }

    private void txtName_TextChanged(object sender, TextChangedEventArgs e)
    {

    }

    private void txtName_GotFocus(object sender, RoutedEventArgs e)
    {
        txtName.Text = "";
    }

    private void txtAge_GotFocus(object sender, RoutedEventArgs e)
    {
        txtAge.Text = "";
    }
}
}
Gajendra Rajput
  • 93
  • 1
  • 13
  • @Gahendra Rajput. What is the exact issue ? – Eldho Sep 03 '15 at 11:20
  • @Eldho When emulator is closed, the data entered is lost by using the above code. when i click view customer button, it does not display the customers previously entered. I want the previously entered data to display. Is it possible by any means? – Gajendra Rajput Sep 04 '15 at 06:42

1 Answers1

1

1.-Permanently Insert and Fetch data from database (I had downloaded a code from some website but after running it when I close the emulator and try to view the data previously entered, it won't return it)

When you close the emulator you lost all apps installet on it, so if you close it, you lost all. If you want test your data save, you can close the application (only de app, not the emulator) and open it from your app list in the WP emulator.

Like it should be stored in phone memory or any such place With SQL lite you can´t store the data in the SD, it will be stored in your app directory, if you want use the SD to store data, you can use binary files

Display the fetched data in listview or grid To show your data in the listview or grid, you need create a ViewModel or DataContext and then use Binding to "send" the data to de view.

Brakyo
  • 128
  • 2
  • 10
  • Hi @Brakyo Considering a real world scenario, As in this case when emulator is closed, the data entered is lost. If I am deploying that particular app in a windows phone will it retain the data entered? or it will be lost? – Gajendra Rajput Sep 04 '15 at 06:30
  • 1
    @GajendraRajput Until the app is uninstalled the data will be available. The database is store in the `IsolatedStorage` of each application. Once the user uninstall the app the `AppData' eg your database will be lost. if user restart the phone your will be still available. The emulator is initializing at each time so it mean a new phone instance , so the already saved data is removed from the memory . Try debugging in a phone. – Eldho Sep 04 '15 at 07:25
  • Thank you @Eldho for this explanation. It significantly clarifies my doubt. – Gajendra Rajput Sep 04 '15 at 08:31
  • @GajendraRajput Yes if you deployed in a physical phone the data will be retained. – Brakyo Sep 04 '15 at 19:36
  • 1
    Thank you @Brakyo for explanation – Gajendra Rajput Sep 07 '15 at 06:52