0

I have comboBox with data from database and DataGrid in tabControl.

Everything working. I select something in comboBox_1 on tabItem_1 (DataGrid_1 show table from DB for my choice value for ComboBox_1) and switch to tabItem_2 and select something in comboBox_2 (DataGrid_2 show table from DB for my choice value for ComboBox_2)

but when I back to TabItem_1 a had error from TabItem_1:

The object reference was not set to the object instance

when I clik 'ok' everything working. How I can fix it?

XAML code:

 <TabItem Background="Bisque" ToolTip="W tym oknie możesz przeglądnąć historię wypożyczeń danej ksiązki, wybierz z ComboBoxa pozycje i przycisk kliknij wczytaj">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal" >
                        <TextBlock Text="Historia książęk"  FontSize="16" />
                    </StackPanel>
                </TabItem.Header>
                <StackPanel>
                    <ComboBox x:Name="cBox_hisBooks"  HorizontalAlignment="Center" Margin="5" VerticalAlignment="Top" Width="300"  ToolTip="Rozwiń liste i wybierz książkę, której historie wypożyczeń chcesz wyświetlić" IsEnabled="True" Loaded ="cBox_hisBooks_Loaded" SelectionChanged="cBox_hisBooks_SelectionChanged"/>
                    <!-- DisplayMemberPath="HISTORies"-->
                    <DataGrid x:Name="dg_hisBooks" AutoGenerateColumns="False" VerticalAlignment="Center" Height="400"  ToolTip="Podgląd bazy danych z ewidencja czytelników wypożyczeń, aby odświeżyć naciśnij przycisk poniżej" Loaded="dg_hisBooks_Loaded" SelectionChanged="dg_hisBooks_SelectionChanged" Margin="10,5">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="ID czytelnika" Binding="{Binding USER.USERID}"/>
                            <DataGridTextColumn Header="Imię czytelnika" Binding="{Binding USER.FIRSTNAME}"/>

CS code:

private void cBox_hisBooks_Loaded(object sender, RoutedEventArgs e)      // ok 
        {
            try         // próba połaczenia z baza Oracle
            {
                List<BOOK> books = new List<BOOK>();
                using (EntitiesDBTest db = new EntitiesDBTest())
                {
                    books = db.BOOKS.OrderBy(b => b.TITLE).ToList();
                }

                var combo_HB = sender as ComboBox; // zmienna przyjmujaca wartosc z ComboBoxa
                combo_HB.ItemsSource = books;       // zrodlo opcji do wyboru
                combo_HB.DisplayMemberPath = "GetTitleWithAuthor";
                combo_HB.SelectedValuePath = "BOOKID";
                // combo_HB.SelectedIndex = 1;        // domyslnie wybrana opcja (domyslnie 0 czyli pierwsza)

                con.Close(); // zamkniecie bazy danych

            }
            catch (Exception Ex)
            {
                MessageBox.Show("Błąd ładowania listy comboBoxa w historii książek\n " + Ex.Message, "Error 1", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

and

    private void cBox_hisBooks_SelectionChanged(object sender, SelectionChangedEventArgs e) // ok
    {
        var selected_cBox_his_book_Item = sender as ComboBox; // przyjmuje wartosc z ComboBoxa
        choiceBook_his = (int)selected_cBox_his_book_Item.SelectedValue; 
if (choiceBook_his == 0)
    MessageBox.Show("Aktualne pole ComboBox ma wartosc -1 lub 0\nWybierz wartośc z listy");}

enter image description here

Adam G.
  • 81
  • 1
  • 10

1 Answers1

0

Loaded is called each time you change selected tab. So you need to check who is null second time Loaded handler method is called.

Mishka
  • 508
  • 3
  • 5
  • when I use button Load that choiceBook_his = 0. I try use `if (choiceUser_his == null) combo.SelectedIndex = 1; ` but it doesn't work. – Adam G. Aug 27 '17 at 16:42
  • Did you try putting breakpoint in Loaded method? see who is null when you get there the second time? – Mishka Aug 27 '17 at 16:55
  • My method `"dg_hisBooks_Loaded"` is empty. I refresh DataGrid when click button `Load` Can I turn off loaded for changed selectet tab ? – Adam G. Aug 27 '17 at 18:16
  • When I put breakpoint i method `btn_hisBooks_Loaded` I don't have null but when I check `cBox_hisUsers_Loaded` have null for `users` in `combo.ItemsSource = users;` – Adam G. Aug 27 '17 at 18:38
  • Finaly I delete this tabItem and put dataGrid with ComboBox to new window. Now I have not any error. – Adam G. Aug 27 '17 at 21:53