ghasem deh

ghasem deh

  • NA
  • 258
  • 39.7k

Errors in the existing database in the project Xamarin !

Nov 29 2016 10:22 AM
Hi guys
I'm gonna use my existing database in the project but I get an error
I used "https://www.nuget.org/packages/sqlite-net-pcl" in my project
 
 
 
 
 this DB class :
 
  1. public static SQLiteConnection sqliteCon()  
  2.         {  
  3.             string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);  
  4.             string dbPath = Path.Combine(path, "fitnessDBupdate.db");  
  5.             CopyDatabaseIfNotExists(dbPath);  
  6.             using (var conn = new SQLiteConnection(dbPath, true))  
  7.             {  
  8.                 return conn;  
  9.             }  
  10.         }  
  11.   
  12.         public bool createMembers()  
  13.         {  
  14.             try  
  15.             {  
  16.                 using (var conn = sqliteCon())  
  17.                 {  
  18.                     conn.CreateTable<Members>();  
  19.                 }  
  20.                 return true;  
  21.             }  
  22.             catch (SQLiteException ex)  
  23.             {  
  24.                 Log.Info("SQLite EX", ex.ToString());  
  25.                 return false;  
  26.             }  
  27.         }  
  28.   
  29.         private static void CopyDatabaseIfNotExists(string dbPath)  
  30.         {  
  31.             try  
  32.             {  
  33.                 string dbName = "fitnessDBupdate.db";  
  34.                 dbPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), dbName);  
  35.                 // Check if your DB has already been extracted.  
  36.                 if (!File.Exists(dbPath))  
  37.                 {  
  38.                     using (BinaryReader br = new BinaryReader(Application.Context.Assets.Open(dbName)))  
  39.                     {  
  40.                         using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))  
  41.                         {  
  42.                             byte[] buffer = new byte[2048];  
  43.                             int len = 0;  
  44.                             while ((len = br.Read(buffer, 0, buffer.Length)) > 0)  
  45.                             {  
  46.                                 bw.Write(buffer, 0, len);  
  47.                             }  
  48.                         }  
  49.                     }  
  50.                 }                  
  51.             }  
  52.             catch (SQLiteException ex)  
  53.             {  
  54.                 Log.Info("SQLite Ex", ex.ToString());  
  55.             }  
  56.         }  
 and in login activity :
  1. using Android.App;  
  2. using Android.OS;  
  3. using Android.Widget;  
  4. using ExsitingSQLite.Resources.DataHelper;  
  5. using System;  
  6. using System.Linq;  
  7.   
  8. namespace ExsitingSQLite  
  9. {  
  10.     [Activity(Label = "ExsitingSQLite", MainLauncher = true, Icon = "@drawable/icon")]  
  11.     public class MainActivity : Activity  
  12.     {  
  13.         Button btnRegister;  
  14.         Button btnLog;  
  15.         EditText edtName;  
  16.         EditText edtPass;  
  17.   
  18.         DataBase db;  
  19.   
  20.         protected override void OnCreate(Bundle bundle)  
  21.         {  
  22.             base.OnCreate(bundle);  
  23.             SetContentView(Resource.Layout.Main);            
  24.             // ????? ????? ??  
  25.             btnRegister = FindViewById<Button>(Resource.Id.btnRegisterPage);  
  26.             btnLog = FindViewById<Button>(Resource.Id.btnLogin);  
  27.             edtName = FindViewById<EditText>(Resource.Id.edtName);  
  28.             edtPass = FindViewById<EditText>(Resource.Id.edtPass);  
  29.             // ????????  
  30.             btnRegister.Click += btnRegister_Click;  
  31.             btnLog.Click += btnLog_Click;  
  32.         }  
  33.   
  34.         private void btnLog_Click(object sender, EventArgs e)  
  35.         {  
  36.             db = new DataBase();  
  37.             var data = db.listMembers();  
  38.             var data2 = data.Where(x => x.fullName == edtName.Text && x.password == edtPass.Text).FirstOrDefault(); //Linq Query    
  39.             if (data2 != null)  
  40.             {  
  41.                 StartActivity(typeof(HomeActivity));  
  42.             }  
  43.             else  
  44.             {  
  45.                 Toast.MakeText(this"??? ?????? ?? ???? ???? ?? ????? ???", ToastLength.Short).Show();  
  46.             }  
  47.         }  
  48.   
  49.         private void btnRegister_Click(object sender, EventArgs e)  
  50.         {  
  51.             StartActivity(typeof(RegisterActivity));  
  52.         }  
  53.     }  
  54. }  
thanks 

Answers (4)