title: Can't upload data from DataTable to Database in WPF .NET 5.0?
body: <p>I am writing an app WPF C# .NET 5.0 that converts numbers from metric to imperial (and vice-versa). I am having a weird issue with data not uploading to the database. I successfully downloaded data from the database, but I cannot display it in my data grid.</p>
  <p>The database is connected via DataSetProj.xsd file.
  Here's how my database looks like:
  <img src="https://i.imgur.com/VrkSVx4.jpg" alt="img" /></p>
  <p>My app has 3 tabs, one for converter, one for datagrid, and one for other stuff. Whenever input in converter changes, it automatically converts, and adds a record to <code>Stats</code> table.</p>
  <p>My app properly reads data from <code>Converters</code>, <code>MetricUnits</code> and <code>ImperialUnits</code> tables (not sure about <code>Stats</code>, because this table is empty).</p>
  <p><strong>FYI</strong> those are my fields I use to communicate with DB:</p>
  <pre><code>        DataSetProj DBProj = new DataSetProj();
          DataSetProjTableAdapters.ConvertersTableAdapter convertersTableAdapter = new();
          DataSetProjTableAdapters.StatsTableAdapter statsTableAdapter = new();
          DataSetProjTableAdapters.ImperialUnitsTableAdapter imperialUnitsTableAdapter = new();
          DataSetProjTableAdapters.MetricUnitsTableAdapter metricUnitsTableAdapter = new();
          DataSetProjTableAdapters.StatsViewTableAdapter statsViewTableAdapter = new();
  </code></pre>
  <p>Here's the launch-time code:</p>
  <pre><code>    convertersTableAdapter.Fill(DBProj.Converters);
      statsTableAdapter.Fill(DBProj.Stats);
      imperialUnitsTableAdapter.Fill(DBProj.ImperialUnits);
      metricUnitsTableAdapter.Fill(DBProj.MetricUnits);
  </code></pre>
  <p>When I want to add a record to my database, this code invokes:</p>
  <pre><code>private void addStat(double input, double output)
          {
              DataSetProj.ConvertersRow usedConverter = DBProj.Converters.Where(x =&gt; x.NameOf == (ConvertersList.SelectedItem as IConverter).ToString()).First();
              DataSetProj.MetricUnitsRow metricUnit;
              // this if statement exists, because ConvertLength has multiple metric units, like kilometre, metre, etc.
              if (ConvertersList.SelectedItem is ConvertLength)
              {
                  metricUnit = DBProj.MetricUnits.Where(x =&gt; x.NameOf == BoxUnitChoice.SelectedItem.ToString()).First();
              }
              else
              {
                  metricUnit = DBProj.MetricUnits.Where(x =&gt; x.NameOf == (ConvertersList.SelectedItem as IConverter).MetricUnit(true)).First();
              }
              DataSetProj.ImperialUnitsRow imperialUnit = DBProj.ImperialUnits.Where(x =&gt; x.NameOf == (ConvertersList.SelectedItem as IConverter).ImperialUnit(true)).First();
              DBProj.Stats.AddStatsRow(DBProj.Stats.Count, DateTime.Now, (metricToImperial ? 1 : 0), metricUnit, imperialUnit, usedConverter, input, output);
          }
  </code></pre>
  <p>And it works - DBProj.Stats.Count increases every time a record is added. However, when I switch to the tab with the data grid (<code>TabStats</code>), it does not show any stats at all. It uses <code>StatsView</code> as a source, not <code>Stats</code> directly (or at least that's how I want it to work).</p>
  <p>This code invokes when I switch to <code>TabStats</code>:</p>
  <pre><code>private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              if ((Tabs.SelectedItem as TabItem) == TabStats)
              {
                  DBProj.Stats.AcceptChanges();
                  statsViewTableAdapter.Fill(DBProj.StatsView);
                  //neither of these 2 lines populate my datagrid
                  StatsDataGrid.ItemsSource = DBProj.StatsView;
                  //StatsDataGrid.ItemsSource = statsViewTableAdapter.GetData();
                  MessageBox.Show($&quot;{DBProj.Stats.Count} this is stat count\n{DBProj.StatsView.Count} and this is statsView&quot;, &quot;test&quot;);
              }
          }
  </code></pre>
  <p>Here's the output of the message box after I input a couple of values (note data grid is empty):
  <img src="https://i.imgur.com/54lyF8X.jpg" alt="msgbox" /></p>
  <p>To my knowledge, it looks like the data is stored locally in DBProj.Stats table, but is not sent back to my database (where <code>StatsView</code> attempts to get data from). However, isn't <code>.AcceptChanges()</code> supposed to send any changes I made?</p>
  <p>How do I send data back to the database using WPF .NET 5.0? All tutorials I found either used EF or only ever referenced <code>.AcceptChanges()</code> method.</p>
  <p>EDIT: I forgot to add, but the data is never saved in the database - every time I run the program, <code>Stats</code> is empty. I want it to save when I switch to <code>TabStats</code> tab, as stated above.</p>