0
votes

this exact same code ran just fine an hour ago. tweaked a couple things, then ctrl+Z back to this state every time i run it on the android emulator now, it hangs and crashes when i try to load the all products page. VS2019 usually helpful, but not in this case: System.InvalidCastException: 'Specified cast is not valid.' - tried to search, wound up here, where i found a few other similarly titled issues, but the cases were way different. no clue what's going on, no error messages from IDE before running. where it it seems to fail EDIT #1: breakpoint seems to be at the END of the the code-behind. EDIT #2: tried iOS and same issue, except now when it crashes, the IDE brings up a window: Main.iOS "UIApplication.Main(args, null, "AppDelegate");" and reads me the same error. - again, confused, as it was all working fine. and i haven't even looked at Main.cs in days, let alone changed it.

public class Product
{
    public string UPC { get; set; }
    public string Brand { get; set; }
    public int Count { get; set; }
    public string Manufacturer { get; set; }
    public string Style { get; set; }
    public string SellPrice { get; set; }
    public double BuyPrice { get; set; }
    public string Market { get; set; }
    public bool Buying { get; set; }
    public string BoxImage { get; set; }
}

<ContentPage.BindingContext>
        <model:Product/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="productsListView">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50"/>
                                <ColumnDefinition Width="75"/>
                                <ColumnDefinition Width="75"/>
                                <ColumnDefinition Width="75"/>
                            </Grid.ColumnDefinitions>
                            <Label Text="{Binding UPC}"
                                       Grid.Column="1" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                            <Label Text="{Binding Brand}"
                                       Grid.Column="2" HorizontalTextAlignment="End" VerticalTextAlignment="Center"/>
                            <Label Text="{Binding Count}"
                                       Grid.Column="3" HorizontalTextAlignment="Start" VerticalTextAlignment="Center"/>
                            <Label Text="{Binding Market}"
                                       Grid.Column="4" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                            <Label Text="{Binding BuyPrice}"
                                       Grid.Column="5" HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>

public partial class AllProductsPage : ContentPage
{
    public AllProductsPage()
    {
        InitializeComponent();
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        {
            using (SQLiteConnection conn = new SQLiteConnection(App.DatabasePath))
            {
                conn.CreateTable<Product>();
                var products = conn.Table<Product>().ToList();
                productsListView.ItemsSource = products;
            }
        }
    }
}
1
Is your SellPrice really a string?Brinky
until i want to use it, call on it, manipulate it, yeah it's an empty string since strings are automatically nullableJuggern Ott
I believe you are missing a column definition.Shak Ham
InvalidCastException is a runtime exception and the stack trace should show exactly which lines causes it. If you can't get a stack trace, then step through it in the debugger until you hit the line that causes the exception.Jason
i thought that too. so i added it back in. still same problem.Juggern Ott

1 Answers

0
votes

i don't know why, but adding <ViewCell></ViewCell> around the <Grid></Grid> seems to get it to work on some level.

at least i can move on now, but i really wish someone could explain. why was it such and issue? why wasn't it clear (in the IDE or the Documentation)?