Published MAUI app doesn't work with access database

57 Views Asked by At

I have a .NET MAUI app that connects to a local access DB (don't ask me why my employer uses access), it works when I run it from Visual Studio in both debug and release modes. When I published it, it doesn't work and it crashes. I don't know how to create a log file for the app to see why it crashes.

I know it crashes when it tries to connect to the database because I added to the app that it should display some text in every part of code it runs, it crashes at the line it tries to connect.

I use a OleDbConnection with connection string

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\access\accessdb.accdb"

I load a data table based on the primary key value passed in to a Entry. When I pass in a string it doesn't crash because the PK needs to a int and I have a if statement to check if it's a int, but when I pass in a int it tries to connect to the database and it crashes.

My xaml code:

<Grid ColumnDefinitions="*,*,*,*" RowDefinitions="Auto,Auto,*">
    <Label x:Name="MainLbl" Text="" FontSize="Header"
           VerticalOptions="Center" HorizontalOptions="Center" 
           Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="2"/>
    <Label Grid.Column="1" Grid.Row="1" Text="Add a ID"/>
    <Entry x:Name="StudentIdTxt" Placeholder="Add here" Grid.Column="2" Grid.Row="1" VerticalOptions="Start" TextChanged="StudentIdTxt_TextChanged"/>
    <Grid x:Name="StudentLst" IsVisible="False" ColumnDefinitions="*,*" RowDefinitions="*,*,*,*,*" Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="2">
        <Label Grid.Column="0" Grid.Row="0" Text="Name"/>
        <Label Grid.Column="1" Grid.Row="0" Text="{Binding StudentName}"/>
    </Grid>
</Grid>

My c# code with the text in label for debugging at what line it crashes. The reason why I added the async and await is because when I added a int it crashed right away and I wanted to debug at what line it actually crashed.

DataTable localDT = new();

public StudentSearch()
{
    InitializeComponent();
    MainLbl.Text = "0";
}

private async void StudentIdTxt_TextChanged(object sender, TextChangedEventArgs e)
{
    await Task.Delay(100);
    MainLbl.Text = "1";
    await Task.Delay(100);
    ShowData();
}

private async void ShowData()
{
    MainLbl.Text = "2";
    await Task.Delay(100);
    if (int.TryParse(StudentIdTxt.Text, out int id))
    {
        MainLbl.Text = "3";
        await Task.Delay(100);
// This is where it crashes in published version and it shuts down the program.
// In debug and in release when running from visual studio it continues and shows the data.
        localDT = Load(id);
        MainLbl.Text = "4";
        await Task.Delay(100);
        if (localDT != null && localDT.Rows.Count > 0)
        {
            MainLbl.Text = "5";
            await Task.Delay(100);
            StudentLst.IsVisible = true;
        }
        else
        {
            StudentLst.IsVisible = false;
        }
    }
    else
    {
        StudentLst.IsVisible = false;
    }
}

private DataTable Load(int primarykeyvalue)
{
    DataTable dt;
    OleDbConnection con = new();
    con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\access\accessdb.accdb";
    con.Open();
    OleDbCommand cmd = con.CreateCommand();

    cmd.CommandText = $"select * from StudentProgramRewards where StudentId = {primarykeyvalue}";
    dt = GetDataTable(cmd);
    return dt;
}

private DataTable GetDataTable(OleDbCommand cmd)
{
    DataTable dt = new();
    OleDbDataAdapter adapter = new(cmd);
    adapter.Fill(dt);
    return dt;
}

I have more code than this so ignore the binding in the Entry. That's not causing the issue.

0

There are 0 best solutions below