Search code examples
visual-studio-2012windows-phone-8windows-phone-8-emulatorwindows-phone-8-sdk

Creating a local database in windows phone app 8 using vs2012


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 = "";
    }
}
}

Solution

  • 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.