0

I am creating a login form using Xamarin which accepts email, password fields.

After the user enters his details and clicks on login button, no alert box is displayed. It comes out of the app. Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlTypes;

using Xamarin.Forms;
using SQLite;
using System.IO;

namespace LoginPage.Views
{
 public class AddDetails : ContentPage
 {
    private Entry _emailEntry;
    private Entry _passwordEntry;
    private Button _saveButton;

    string _dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "myDB.db3");
    public AddDetails()
    {
        this.Title = "Add Details";
        StackLayout stackLayout = new StackLayout();



        _emailEntry = new Entry();
        _emailEntry.Keyboard = Keyboard.Text;
        _emailEntry.Placeholder = "Email";
        stackLayout.Children.Add(_emailEntry);


        _passwordEntry = new Entry();
        _passwordEntry.Keyboard = Keyboard.Text;
        _passwordEntry.Placeholder = "Password";
        stackLayout.Children.Add(_passwordEntry);

        _saveButton = new Button();
        _saveButton.Text = "Login User";
        _saveButton.Clicked += _saveButton_Clicked;
        stackLayout.Children.Add(_saveButton);

        Content = stackLayout;
    }

    private async void _saveButton_Clicked(object Sender, EventArgs e)
    {
        var db = new SQLiteConnection(_dbPath);
        db.CreateTable<User>();

        var maxPk = db.Table<User>().OrderByDescending(c => c.Id).FirstOrDefault();

        User user = new User()
        {
            Id = (maxPk == null ? 1 : Convert.ToInt32(maxPk.Id) +1),
            Email = _emailEntry.Text,
            Password = _passwordEntry.Text

        };

        db.Insert(user);
        await DisplayAlert(null, user.Email + "Saved", "Ok");
        await Navigation.PopAsync();
     }

     }
     }

Login screen gets displayed, but no alert box which should say Email saved. Also, it comes out of the app abruptly

user
  • 1,681
  • 5
  • 18
  • 42
  • if the app is crashing you need to determine the cause. Use try/catch to catch the exception causing the crash, or use the debugger to catch it. – Jason Sep 01 '19 at 13:39

1 Answers1

0

Have you set MainPage = new NavigationPage (new Page1Xaml ());

Please follow this link https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/

Chetan Rawat
  • 578
  • 3
  • 17