0

Anyone have an idea why the line FullNameTextBox.Text = row[0].ToString(); causes a NullReferenceException whenever dataTable.Clear() is called? I can't use TableAdapter.Fill() because of this. The ta.Insert would work fine until you click on a row on the datagrid. Changing FullNameTextBox into another text box that's not within the ta.Insert line doesn't work either.

    private DataRowView row;
    private byte[] xByte;
    TestDataSetTableAdapters.TestingTableTableAdapter ta = new TestDataSetTableAdapters.TestingTableTableAdapter();
    TestDataSet ts = new TestDataSet();

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        ta.Insert(FullNameTextBox.Text, xByte, ColumnTwoTextBox.Text, ColumnThreeTextBox.Text);
        ta.Fill(ts.TestingTable);
    }

    private void TestGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        row = (DataRowView)TestGrid.SelectedItem;
        FullNameTextBox.Text = row[0].ToString(); //this would cause the exception removing this line would make the Add Button work even when the SelectionChanged event has been triggered
    }
Dog
  • 25
  • 4
  • `.Clear();` removes all content, this causes the `SelectionChanged` event to fire, `row[0]` doesn't exist, exception thrown. – Equalsk Sep 26 '17 at 10:57
  • I didn't know that the row would become null once .Clear() is called. Adding a if(row != null) before the row[0].ToString(); line worked. Thanks – Dog Sep 26 '17 at 11:20

0 Answers0