title: How to save a list of objects in a SQLite database and read it to retrieve it?
body: <p>I am currently making an application in C #. I am trying to save a user's data in a SQLite database. This user has a name, a first name, a username, a password, a balance and a list of programs. I can save everything in the database except the program list. Here is the code that I have now to record all this :</p>
  
  <pre class="lang-cs prettyprint-override"><code> SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionString);
  
                  try
                  {
                      sqliteCon.Open();
                      string Query = "Update tblUser Set Balance ='"+CurrentUser.Balance+"',ProgramList='"+CurrentUser.ProgramList+"' Where UserName='"+CurrentUser.UserName+"'";
                      SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
                      createCommand.ExecuteNonQuery();
                      sqliteCon.Close();
                      this.Close();
  
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
  </code></pre>
  
  <p>The problem is when I want to recover the data I have an error message saying: Can not cast an object of type "System.String" type "System.Collection.Generic.List [ClassLibrary1. Program]</p>
  
  <p>Here is how my table is structured:</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE "tblUser" (
      "ID"    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
      "FirstName" TEXT NOT NULL,
      "LastName"  TEXT NOT NULL,
      "Username"  TEXT NOT NULL UNIQUE,
      "Password"  TEXT NOT NULL,
      "Balance"   INT NOT NULL,
      "ProgramList"   LIST
  );
  </code></pre>
  
  <p>Here is the code when I try to recover the data :</p>
  
  <pre><code>  SQLiteConnection sqliteCon = new SQLiteConnection(dbConnectionstring);
              try
              {
                  sqliteCon.Open();
                  string Query = "select * from tblUser where UserName='" + this.txtUsername.Text + "' and PassWord='" + this.txtPassword.Password + "'";
                  SQLiteCommand createCommand = new SQLiteCommand(Query, sqliteCon);
  
                  createCommand.ExecuteNonQuery();
                  SQLiteDataReader dr = createCommand.ExecuteReader();
  
                  int count = 0;
                  while (dr.Read())
                  {
                      User user = new User();
                      User.Username = (string)dr[3];
                      User.FirstName = (string)dr[1];
                      User.LastName = (string)dr[2];
                      User.Balance = (int)dr[5];
                      User.ProgramList = (List&lt;Programme&gt;)dr[6] ;
                      CurrentUser = User;
                  }
  ...
  </code></pre>
  
  <p>I can recover everything except the list ...
  I know that the mistake comes from : User.ProgramList = (List)dr[6] ; and from "ProgramList" LIST</p>
  
  <p>To answer Ben : </p>
  
  <pre class="lang-cs prettyprint-override"><code>public class User
      {
  
          public string UserName { get; set; }
          public string FirstName{ get; set; }
          public string LastName { get; set; }
          public int Balance { get; set; }
          public List&lt;Program&gt; ProgramList { get; set; }
  
  
          public User() { }
          public User(string username, string firstname, string lastname, int balance,List&lt;Program&gt; programlist)
          {
              this.UserName = username;
              this.FirstName = firstname;
              this.LastName = lastname;
              this.Balance = balance;
              this.ProgramList = programlist;
          }
  </code></pre>