vivek shahi
vivek shahi

Reputation: 39

Using Linq to sql for windows phone

I am trying to show out History for my windows 8 application which includes date, time , Floor ,Zone , Longitutde and latitude. I tried the code below but there was no output in it above.

THe image you can see on the link down below that I want to show on my application.But I can see nothing when I run my program.

I have three classes for using linq to sql to retrieve database and show information through it.The main class is History.xaml.cs

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 System.IO.IsolatedStorage;   
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Text;
using System.Data.Linq;


namespace SmartParking
{

public partial class History : PhoneApplicationPage
{
    private readonly HistoryDataContext historylog; 
    public History()
    {
        InitializeComponent();
       // createDB();





    }

    public HistoryDataContext Log
    {
        get { return historylog; }
    }

    public void createDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            if (historylog.DatabaseExists() == false)
            {
                historylog.CreateDatabase();
                addDataDB();
            }
        }

    }

    public void addDataDB()
    {
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            HistoryDB hdb = new HistoryDB
            {
               // Date = DateTime.Today,
               // Time = DateTime.Now.TimeOfDay,
                Zone = Checkin.Zone_st,
                Floor = Checkin.Floor_st,
                location_latitude = Checkin.Latitud_do,
                location_longtitud = Checkin.Longtitude_do
            };
            historylog.history.InsertOnSubmit(hdb);
            historylog.SubmitChanges();
            GetHistoryLog();

        }
    }
    public IList<HistoryDB> GetHistoryLog()
    {
        IList<HistoryDB> HistoryList = null;
        using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
        {
            IQueryable<HistoryDB> query = from histoy in historylog.history select histoy;
            HistoryList = query.ToList();
        }
        return HistoryList ;
    }


}
}

The next class is HistoryDB.cs with the tables and columns

[Table]
    public class HistoryDB 
{

    [Column(CanBeNull = false)]
    public DateTime Date
    { get; set; }

    [Column(CanBeNull = false)]
    public TimeSpan Time
    { get; set; }

    [Column(CanBeNull = false)]
    public String Zone
    { get; set; }

    [Column(CanBeNull = false)]
    public String Floor
    { get; set; }

    [Column(CanBeNull = false)]
    public double location_latitude
    { get; set; }

    [Column(CanBeNull = false)]
    public double location_longtitud
    { get; set; }

}

The next class is HistoryDataContext.cs.

public class HistoryDataContext:DataContext 
{
    public static string DBConnectionString = "Data Source=isostore:/History.sdf";
    public HistoryDataContext(string DBConnectionString)
        : base(DBConnectionString)
    {
}

   public Table<HistoryDB> history
    {
        get
        {
            return this.GetTable<HistoryDB>();
        }
    }


}

What I am trying is to get information from the NFC tag and store it in the local database and then retrieve it in the history page. The code below is to read from nfc tag but I dunno how again to save in the local databse from there and retrive it back in the history page.

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 System.Runtime.InteropServices.WindowsRuntime;
using Windows.Networking.Proximity;
using NdefLibrary.Ndef;
using NdefLibraryWp.Ndef;
using Windows.Networking.Sockets; // needed for DataReader, DataWriter
using Windows.UI.Popups;
using Microsoft.Phone.UserData;
using System.Text;
using Windows.Phone.PersonalInformation;
using SmartParking.Resources;
using System.Diagnostics;


namespace SmartParking
{

public partial class Checkin : PhoneApplicationPage
{
    private ProximityDevice _device;
    private long _subscriptionIdNdef;

    public static double Latitud_do { get; set; }
    public static double Longtitude_do { get; set; }
    public static string Floor_st { get; set; }
    public static string Zone_st { get; set; }

    History store = new History();


    public Checkin()
    {
        InitializeProximityDevice();
        InitializeComponent();


    }

    private void SetLogStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (LogStatus != null) LogStatus.Text = newStatus; });
    }

    private void SetFloorStatus(string newStatus)
    {
        Dispatcher.BeginInvoke(() => { if (FloorStatus != null) FloorStatus.Text = newStatus; });
    }



    private void ApplicationBarIconButton_Click(object sender, System.EventArgs e)
    {
        MessageBox.Show(" ");
    }


    private void InitializeProximityDevice()
    {
        _device = Windows.Networking.Proximity.ProximityDevice.GetDefault();
        if (_device != null)
        {
            _subscriptionIdNdef = _device.SubscribeForMessage("NDEF", MessageReceivedHandler);

        }

    }


    private void MessageReceivedHandler(ProximityDevice sender, ProximityMessage message)
    {
        var rawMsg = message.Data.ToArray();
        var ndefMessage = NdefMessage.FromByteArray(rawMsg);


        ////// Loop over all records contained in the NDEF message
        foreach (NdefRecord record in ndefMessage)
        {

            if (NdefTextRecord.IsRecordType(record))
            {
                // Convert and extract URI info
                var textRecord = new NdefTextRecord(record);
                //var str = textRecord.Text;
                string[] str = textRecord.Text.Split('|');

                var latitude = str[2];
                Latitud_do = double.Parse(latitude);
                var longtitude = str[3];
                Longtitude_do = double.Parse(longtitude);
                var Floor_st = str[0];
                var Zone_st = str[1];


                SetLogStatus("Floor: " + Floor_st + " Zone: " + Zone_st );
                SetFloorStatus("Longitude: " + latitude + "   Longitude: " + longtitude);
                store.addDataDB();

            }
        }
      }
 }

}

The Image of the table is in the link below

https://www.dropbox.com/s/g87yta6hegjstge/Untitled.png?dl=0

Upvotes: 0

Views: 626

Answers (1)

Amit Bhatiya
Amit Bhatiya

Reputation: 2621

try this:

In Database There Should be a Primary key otherwise it would Throw an Exception and the Datatype that is TimeSpan not supported so you need to take DateTime or String as Done Below :

[Table]
    public class HistoryDB
    {
        [Column(IsPrimaryKey = true)]
        public int Id { get; set; }

        [Column(CanBeNull = false)]
        public DateTime Date
        { get; set; }

        [Column(CanBeNull = false)]
        public DateTime Time
        { get; set; }

        [Column(CanBeNull = false)]
        public String Zone
        { get; set; }

        [Column(CanBeNull = false)]
        public String Floor
        { get; set; }

        [Column(CanBeNull = false)]
        public double location_latitude
        { get; set; }

        [Column(CanBeNull = false)]
        public double location_longtitud
        { get; set; }

    }

        public void addDataDB()
        {
            using (HistoryDataContext historylog = new HistoryDataContext(HistoryDataContext.DBConnectionString))
            {
                HistoryDB hdb = new HistoryDB
                {
                    Id = 0,
                     Date = DateTime.Today,
                    Time = DateTime.Now,
                    Zone = "Zone",
                    Floor = "Floore",
                    location_latitude = 00.00,
                    location_longtitud = 00.00
                };
                historylog.history.InsertOnSubmit(hdb);
                historylog.SubmitChanges();
                GetHistoryLog();

            }
        }

I have implemented above thing and its working Properly

Upvotes: 1

Related Questions