dimanche 17 janvier 2016

EditText to sqlitenet with xamarin

Im new here.

Quick question, i'll like to extract the values from my inputs and in my table user but it doesn't work. It just takes the default android:text under XAML and put them in instead. What am i doing wrong? Much kind regards and thanks for your time.

        SetContentView(Resource.Layout.create_user);
        inputEmailText = FindViewById<EditText>(Resource.Id.inputCreateEmail);
        inputUsernameText = FindViewById<EditText>(Resource.Id.inputCreateUsername);
        inputPasswordText = FindViewById<EditText>(Resource.Id.inputCreatePassword);
        createUserBtn = FindViewById<Button>(Resource.Id.btnCreateUser);
        showUsersBtn = FindViewById<Button>(Resource.Id.btnShowUsers);

        var tEmail = inputEmailText.Text;
        var tUsername = inputUsernameText.Text;
        var tPassword = inputPasswordText.Text;

        string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"users.db4"); //change til db3 change it to another version
        var db = new SQLiteConnection(dbPath);
        db.CreateTable<User>();


        // Insert note into the database


        // Show the automatically set ID and message.




        createUserBtn.Click += delegate {
            //  StartActivity(typeof(LoginHomeActivity));
            if (db.Table<User>().Count() == 0)
            {
            user = new User () { Email = tEmail, Username = tUsername, Password = tPassword };
            db.Insert(user);

            }
            else
            {
                Toast.MakeText(this, "The username or email already exist", ToastLength.Short).Show();
            }
        };



        showUsersBtn.Click += delegate {
            //  StartActivity(typeof(LoginHomeActivity));

            var table = db.Query<User>("SELECT * FROM User");
            foreach (var s in table)
            {
                Console.WriteLine(s.Id + " " + s.Email + " " + s.Username + " " + s.Password);
                Toast.MakeText(this, s.Username, ToastLength.Long).Show();
            }

        };

Aucun commentaire:

Enregistrer un commentaire