WP8 No Such a table Sqlite - windows-phone-8

i m trying to develop windows phone application but i have a problem about sqlite. I couldn't connect database and app. I had error message like this "no such table: hastalik". If some 1 have any idea, please share. Thanks for your help.
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;
using Windows.Storage;
using SQLite;
namespace illnessTracker
{
public partial class Page1 : PhoneApplicationPage
{
public static string DB_PATH = Path.Combine(Path.Combine(ApplicationData.Current.LocalFolder.Path, "ilac.sqlite"));
public Page1()
{
InitializeComponent();
}
private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
using (SQLiteConnection dbConn = new SQLiteConnection("Data Source=ilac.sqlite; FailIfMissing=True"))
{
// Create a new task.
hastalik hasta = new hastalik();
hasta.hastalikAdi = txtbxHastalikAdi.Text;
hasta.semptomlar = txtbxSemptomlar.Text;
hasta.ilaclar = tbxIlaclar.Text;
hasta.not = tbxNot.Text;
hasta.doktorTavsiyesi = tbxTavsiye.Text;
hasta.tarihi = DateTime.Now.Date;
/// Insert the new task in the Task table.
dbConn.Insert(hasta);
}
}
[Table("hastalik")]
public class hastalik
{
[Column("hastalikID")]
[PrimaryKey, AutoIncrement]
public int hastalikID { get; set; }
[Column("hastalikAdi")]
public string hastalikAdi { get; set; }
[Column("semptomlar")]
public string semptomlar { get; set; }
[Column("ilaclar")]
public string ilaclar { get; set; }
[Column("doktorTavsiyesi")]
public string doktorTavsiyesi { get; set; }
[Column("not")]
public string not { get; set; }
[Column("tarihi")]
public DateTime tarihi { get; set; }
}
}
}

Related

crud operation with dynamic data with 3 level hierarchy in json file in asp .net core web api

I want to do get, post, put and delete in it using asp.net core Web API. But I should not use database to store the data instead I need to store the dynamic data in controller using method and also in json file list of user(userId,userName),3 level hierarchy(Country should have list of state,State should have list of city,And I could add Countries along with its states and cities) and use it to do the http action. Can any one please help me with some step or code?
I create a sample demo for you, and I suggest you don't use json file.
Why:
When you use json files, IO operations are required, and there will be a problem of exclusive use of files.
In addition, when adding, deleting, modifying and checking the content of the file, every time there is a json file that is read, then converted into an object, and then written into the file, the efficiency will be very low.
Because what you want is a demo, the test code I wrote does not have any verification. You can pay attention to the places that need attention.
AddSingleton must be used when registering a service, so as to ensure that all users access the same data source.
When the amount of data is very large and there are too many requests, there will be a situation where the data does not match. Because I have not added any locks or restrictions here.
Test Result
Test Code:
Create GlobalVariablesService, and register it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DB_Project.Models
{
public class GlobalVariablesService
{
public List<RecordModel> records = new List<RecordModel>();
public GlobalVariablesService(){
}
public List<RecordModel> AddRecord(RecordModel r) {
records.Add(r);
return records;
}
public List<RecordModel> RemoveRecord(int rid)
{
var itemToRemove = records.Single(r => r.Record_ID == rid);
records.Remove(itemToRemove);
return records;
}
public RecordModel GetRecord(int rid)
{
var itemToGet = records.Single(r => r.Record_ID == rid);
return itemToGet;
}
}
/// <summary>
/// Add Record History
/// Rid #Uid #Country_id #Country_Name #State_id #State_Name #City_id #City_Name
/// 1 001 C1 Country1 S1 State1 C_1 City1
/// 2 002 C2 Country2 S2 State2 C_2 City2
/// 3 003 C3 Country3 S3 State3 C_3 City3
/// </summary>
///
public class RecordModel {
public int Record_ID { get; set; }
public int Uid { get; set; }
public string Country_id { set; get; }
public string Country_Name { set; get; }
public string State_id { set; get; }
public string State_Name { set; get; }
public string City_id { set; get; }
public string City_Name { set; get; }
}
public class UserModel {
public int Uid { set; get; }
public string Name { set; get; }
}
public class CountryModel
{
public string Country_id { set; get; }
public string Country_Name { set; get; }
public List<StateModel> State { set; get; }
}
public class StateModel
{
public string State_id { set; get; }
public string State_Name { set; get; }
public List<CityModel> City { set; get; }
}
public class CityModel
{
public string City_id { set; get; }
public string City_Name { set; get; }
}
}
Register service in Startup.cs file;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton<GlobalVariablesService>();
}
My Test Controller
using DB_Project.DbClass;
using DB_Project.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DB_Project.Controllers
{
public class TestController : Controller
{
private readonly ILogger<TestController> _logger;
private readonly GlobalVariablesService _service;
public TestController(ILogger<TestController> logger, GlobalVariablesService service)
{
_logger = logger;
_service = service;
}
public IActionResult get(int rid)
{
try
{
var model = _service.GetRecord(rid);
return Ok(model);
}
catch (Exception e)
{
return Ok("error occured :" + e.ToString());
throw;
}
}
public string add(RecordModel r)
{//string uid,string countryid,string countryname,string stateid,string statename,string cityid,string cityname ) {
try
{
_service.AddRecord(r);
return "success";
}
catch (Exception)
{
return "failed";
throw;
}
}
public string delete(int rid){
try
{
_service.RemoveRecord(rid);
return "success";
}
catch (Exception)
{
return "failed";
throw;
}
}
}
}

Cannot deserialize - JSON.NET - Xamarin

I'm struggling with below message. Other JSON objects in the same app work correctly.
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array
(e.g. [1,2,3]) into type 'Monkeys.Models.Rootobject' because the type requires a JSON
object (e.g. {"name":"value"}) to deserialize correctly.
I have the following output from the API via web browser:
[{"Id":"1","Name":"Monkey 1","Location":null,"Details":null,"SKU":null,"Size":null,
"Color":null,"Image":null,"Brand":null,"NameSort":"M"},{"Id":"2","Name":"Monkey 2",
"Location":null, "Details":null,"SKU":null,"Size":null,"Color":null,"Image":null,
"Brand":null,"NameSort":"M"}]
The Model in my App:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Monkeys.Models
{
public class Monkey
{
public string Id { get; set; }
public string Name {get;set;}
public string Location { get; set; }
public string Details { get; set; }
//URL for our monkey image!
public string SKU { get; set; }
public string Size { get; set; }
public string Color { get; set; }
public string Image { get; set; }
public string Brand { get; set; }
public string NameSort
{
get
{
if (string.IsNullOrWhiteSpace(Name) || Name.Length == 0)
return "?";
return Name[0].ToString().ToUpper();
}
}
}
public class Rootobject
{
public Monkey[] monkeys { get; set; }
}
}
The App:
using System;
using System.Net;
using Newtonsoft.Json;
using System.Threading.Tasks;
using Monkeys.Models;
using System.Collections.Generic;
namespace Monkeys.Apis
{
public class GetMonkeys
{
public GetMonkeys()
{
}
public async Task<Monkey[]> GetMonkeysAsync()
{
var client = new System.Net.Http.HttpClient();
client.BaseAddress =
new Uri("https://microsoft- apiappce13ac35390d40a684dd6ab72a9eef8f.azurewebsites.net:443/");
var response = await client.GetAsync("api/Monkey");
var earthquakesJson = response.Content.ReadAsStringAsync().Result;
var rootobject = JsonConvert.DeserializeObject<Rootobject> (earthquakesJson);
return rootobject.monkeys;
}
}
}
Your json data is an array of Monkey. Try this
// monkeys will be a List<Monkey>
var monkeys = JsonConvert.DeserializeObject<List<Monkey>>(earthquakesJson);

Read Object from JSON web page in Windows Phone 8.1

I want to read some List<> of Object from a JSON file, which is generated by a PHP file.
When I try to compile it, I have some problems, it seems that the Smartphone is waiting something. Also I'm not sure if the read is correct for Windows Phone. Thank you in advance for your help !
This is the JSON file example:
{"giocatori":[{"Giocatore":"124","Cognome":"DE SANCTIS","Ruolo":"P","Squadra":"ROM"},{"Giocatore":"140","Cognome":"MIRANTE","Ruolo":"P","Squadra":"PAR"},{"Giocatore":"156","Cognome":"SKORUPSKI","Ruolo":"P","Squadra":"ROM"}],"success":1}
These are the Objects:
public class Giocatori
{
public string Giocatore { get; set; }
public string Cognome { get; set; }
public string Ruolo { get; set; }
public string Squadra { get; set; }
public override string ToString()
{
return Giocatore + " " + Cognome + " " + Ruolo + " " + Squadra;
}
}
public class RootObject
{
public List<Giocatori> giocatori { get; set; }
public int success { get; set; }
}
And here are the methods:
private async Task<RootObject> getPlayers()
{
Uri serviceUri = new Uri("myURL");
HttpClient client = new HttpClient();
string jsonString = await client.GetStringAsync(serviceUri);
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
RootObject RootObject = new RootObject();
DataContractJsonSerializer ser = new DataContractJsonSerializer(RootObject.GetType());
RootObject = ser.ReadObject(ms) as RootObject;
return RootObject;
}
private void loadPlayers()
{
RootObject players = getPlayers().Result;
setComboboxs(players.giocatori); // The method which I need to use the
}
Download JSON.Net package via Nuget.
Right Click project > Manage Nuget > Json.net > Install
According to http://json2csharp.com/ your class should look like this.
public class Giocatori
{
public string Giocatore { get; set; }
public string Cognome { get; set; }
public string Ruolo { get; set; }
public string Squadra { get; set; }
}
public class RootObject
{
public List<Giocatori> giocatori { get; set; }
public int success { get; set; }
}
RootObject can be renamed to whatever you like.
When you receive your JSON do
JsonConvert.DeserializeObject<RootObject>("jsonstring");

Cascade Delete for MySQL and ASP.NET MVC4

I have create simple asp.net (MVC4) web page with mysql database. There I have two tables (persons and orders), where table orders have FORIGN Key Persons_ID. I whant to create delete function, so, when I delete person from persons table, it will also delete all orders from order table for this person.
For creating models I have used ADO.NET and it create this two models for each tables:
persons.cs
using System.ComponentModel.DataAnnotations;
namespace MvcMySQLTest1.Models
{
using System;
using System.Collections.Generic;
public partial class person
{
public person()
{
this.orders = new HashSet<order>();
}
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public string Adress { get; set; }
public string City { get; set; }
public virtual ICollection<order> orders { get; set; }
}
}
orders.cs
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace MvcMySQLTest1.Models
{
using System;
using System.Collections.Generic;
public partial class order
{
public int O_Id { get; set; }
public int OrderNo { get; set; }
public Nullable<int> Persons_Id { get; set; }
public virtual person person { get; set; }
}
}
I have also create MainModel - like I container for both models above:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace MvcMySQLTest1.Models
{
public class MainModel
{
public person Persons { get; set; }
public order Orders { get; set; }
}
}
Now for Cascade deleting I have try this - so when I delete Person, it will also delete all Orders for this Person in order table, but this seems not to work:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
namespace MvcMySQLTest1.Models
{
public class MainModel : DbContext //added
{
public person Persons { get; set; }
public order Orders { get; set; }
//added
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<orders>()
.HasOptional(a => a.persons)
.WithOptionalDependent()
.WillCascadeOnDelete(true);
base.OnModelCreating(modelBuilder);
}
}
}
May be you can try something like this
modelBuilder.Entity<order>()
.HasRequired(a => a.person)
.WithMany(t => t.order)
.HasForeignKey(d => d.Persons_Id)
.WillCascadeOnDelete(true);
Or try to read more here MSDN Cascade Delete. May something can help from there

Deserializing Jarray in JSON.NET

i need to deserialize this.
{"previous_cursor_str":"0","next_cursor":0,"ids":[741999686,240455509,126524150,143548100,124328422,624776268,393738125,587829914,280834485,64818350,282713007,90425850,759794,164401208,114771958,114364910,89725893],"previous_cursor":0,"next_cursor_str":"0"}
any idea?
Its a JObject really with an array of Id's inside it.
First you can create a class to represent the json like this:
public class RootObject
{
public string previous_cursor_str { get; set; }
public int next_cursor { get; set; }
public List<int> ids { get; set; }
public int previous_cursor { get; set; }
public string next_cursor_str { get; set; }
}
Then to deserialize the json into the object you do this:
var myJsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);
Or if you just want the ids in a array:
var obj = JObject.Parse(jsonstring);
var idArray = obj["ids"].Children().Select(s=>s.value<string>());
Just tried https://jsonclassgenerator.codeplex.com/ and got the code below. Which is qhite the same as geepie's class. nice tool.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Example
{
class Result
{
[JsonProperty("previous_cursor_str")]
public string PreviousCursorStr { get; set; }
[JsonProperty("next_cursor")]
public int NextCursor { get; set; }
[JsonProperty("ids")]
public IList<int> Ids { get; set; }
[JsonProperty("previous_cursor")]
public int PreviousCursor { get; set; }
[JsonProperty("next_cursor_str")]
public string NextCursorStr { get; set; }
}
public static unsafe void Main()
{
Result result = JsonConvert.DeserializeObject<Result> (" ... your string ...");
Console.WriteLine(result);
}
}