title: C# WPF DataGrid connecting to a SQLite database
body: <p>I'm new to C# and WPF so I would appreciate your help.</p>
  
  <p>I can't get a WPF grid to connect to a SQLite database and display its data. I just get the Grid displayed without any data.</p>
  
  <p><a href="https://i.stack.imgur.com/oWciY.png" rel="nofollow noreferrer">Blank DataGrid</a></p>
  
  <p>The Datagrid code on Home.xaml:</p>
  
  <pre><code>    &lt;DataGrid Name="DataGridHome" Margin="10,66,242,185" Grid.Row="2" Grid.Column="0" AutoGenerateColumns="False" CanUserAddRows="False"
            CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False"
            CanUserResizeRows="False" CanUserSortColumns="False" SelectionMode="Single" SelectionChanged="DataGrid_SelectionChanged"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="id" Visibility="Hidden"/&gt;
              &lt;DataGridTextColumn Header="Surname" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="First Name" FontFamily="Arial" /&gt;
              &lt;DataGridTextColumn Header="DoorNum" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="StreetName" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="StreetName2" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="City" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="PostCode" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="Belt" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="AccountType" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="JoinDate" FontFamily="Arial"/&gt;
              &lt;DataGridTextColumn Header="MembershipActive" FontFamily="Arial"/&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>The App.config code (my database doesn't have any username or password):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
   &lt;connectionStrings&gt;
    &lt;add connectionString="Data Source=MemberDB.db; Initial Catalog=Pubs;" name="ConString"/&gt;
    &lt;/connectionStrings&gt;
   &lt;/configuration&gt;
  </code></pre>
  
  <p>The C# code Home.xaml.cs :</p>
  
  <pre><code>using System;
  using System.Windows;
  using System.Windows.Controls;
  using System.Data;
  using System.Data.SqlClient;
  using System.Data.SQLite;
  using System.IO;
  
  namespace KarateClub
  {
  /// &lt;summary&gt;
  /// Interaction logic for Home.xaml
  /// &lt;/summary&gt;
  public partial class Home : Page
  {
      public Home()
      {
          InitializeComponent();
          DataBaseConnection();
      }
  
      private void DataBaseConnection()
      {
          try
          {
              SQLiteConnection sqlite = new SQLiteConnection("Data Source=MemberDB.db; Version=3;");
  
              SQLiteCommand sqlcmd;
              sqlite.Open();
              sqlcmd = sqlite.CreateCommand();
              string query = "SELECT * FROM Members";
              sqlcmd.CommandText = query;
              SQLiteDataAdapter sda = new SQLiteDataAdapter(sqlcmd);
              DataTable dt = new DataTable("Members");
              sda.Fill(dt);
              DataGridHome.ItemsSource = dt.DefaultView;
              sqlite.Close();
  
          }
          catch(SQLiteException ex)
          {
              Console.WriteLine("SQLite DataBase Error!");
          }
  
      }
      ....
  </code></pre>
  
  <p>I'm thinking there must be something wrong with App.config or my connection to the database in the C# code. I don't know how name="ConString" in App.config is supposed to be used with the C# code to access SQLite.</p>
  
  <p>This has been VERY hard to research online - some people using Dapper, others using ADO.NET, the ones who have similar code to me are not using a DataGrid but a simple list view.</p>
  
  <p>Thanks :)</p>