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 :
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);
}
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]
Here is how my table is structured:
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
);
Here is the code when I try to recover the data :
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<Programme>)dr[6] ;
CurrentUser = User;
}
...
I can recover everything except the list ... I know that the mistake comes from : User.ProgramList = (List)dr[6] ; and from "ProgramList" LIST
To answer Ben :
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<Program> ProgramList { get; set; }
public User() { }
public User(string username, string firstname, string lastname, int balance,List<Program> programlist)
{
this.UserName = username;
this.FirstName = firstname;
this.LastName = lastname;
this.Balance = balance;
this.ProgramList = programlist;
}
Userclass? In particularUser.ProgramList. The C# class, not the SQL table definition.