How to fill datagrid from SQLite?
up vote
0
down vote
favorite
After I created SQLite database on button click with code like this:
private void button_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE airports (id integer primary key , airportName varchar(100) not null, airportCity varchar(45) not null, active tinyint(1));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (1, 'TestAirport1', 'TestCity1', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (2, 'TestAirport2', 'TestCity2', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our table ?
// First lets build a SQL-Query again:
sqlite_cmd.CommandText = "SELECT * FROM airports";
// Now the SQLiteCommand object can give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the result lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
//System.Console.WriteLine(sqlite_datareader["text"]);
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
}
Everything is fine, database created, table created and some data inserted to table. (I check all with SQLite Manager on MozzilaFirefox)
My goal is to fill DataGrid on button click with data from database like this:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;New=True;";
string query = String.Format("SELECT * FROM airports WHERE active = '1'");
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
dataAdapter.Fill(data);
dgAirports.DataContext = data;
}
But then visual studio throw exception that table 'airports' doesen't exist and for real, the whole database become empty.
How to fix that?
c# database wpf sqlite datagrid
add a comment |
up vote
0
down vote
favorite
After I created SQLite database on button click with code like this:
private void button_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE airports (id integer primary key , airportName varchar(100) not null, airportCity varchar(45) not null, active tinyint(1));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (1, 'TestAirport1', 'TestCity1', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (2, 'TestAirport2', 'TestCity2', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our table ?
// First lets build a SQL-Query again:
sqlite_cmd.CommandText = "SELECT * FROM airports";
// Now the SQLiteCommand object can give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the result lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
//System.Console.WriteLine(sqlite_datareader["text"]);
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
}
Everything is fine, database created, table created and some data inserted to table. (I check all with SQLite Manager on MozzilaFirefox)
My goal is to fill DataGrid on button click with data from database like this:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;New=True;";
string query = String.Format("SELECT * FROM airports WHERE active = '1'");
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
dataAdapter.Fill(data);
dgAirports.DataContext = data;
}
But then visual studio throw exception that table 'airports' doesen't exist and for real, the whole database become empty.
How to fix that?
c# database wpf sqlite datagrid
2
What do you thinkNew=True;
does?
– mjwills
Nov 11 at 9:03
2
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
After I created SQLite database on button click with code like this:
private void button_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE airports (id integer primary key , airportName varchar(100) not null, airportCity varchar(45) not null, active tinyint(1));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (1, 'TestAirport1', 'TestCity1', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (2, 'TestAirport2', 'TestCity2', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our table ?
// First lets build a SQL-Query again:
sqlite_cmd.CommandText = "SELECT * FROM airports";
// Now the SQLiteCommand object can give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the result lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
//System.Console.WriteLine(sqlite_datareader["text"]);
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
}
Everything is fine, database created, table created and some data inserted to table. (I check all with SQLite Manager on MozzilaFirefox)
My goal is to fill DataGrid on button click with data from database like this:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;New=True;";
string query = String.Format("SELECT * FROM airports WHERE active = '1'");
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
dataAdapter.Fill(data);
dgAirports.DataContext = data;
}
But then visual studio throw exception that table 'airports' doesen't exist and for real, the whole database become empty.
How to fix that?
c# database wpf sqlite datagrid
After I created SQLite database on button click with code like this:
private void button_Click(object sender, RoutedEventArgs e)
{
SQLiteConnection sqlite_conn;
SQLiteCommand sqlite_cmd;
SQLiteDataReader sqlite_datareader;
// create a new database connection:
sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");
// open the connection:
sqlite_conn.Open();
// create a new SQL command:
sqlite_cmd = sqlite_conn.CreateCommand();
// Let the SQLiteCommand object know our SQL-Query:
sqlite_cmd.CommandText = "CREATE TABLE airports (id integer primary key , airportName varchar(100) not null, airportCity varchar(45) not null, active tinyint(1));";
// Now lets execute the SQL ;D
sqlite_cmd.ExecuteNonQuery();
// Lets insert something into our new table:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (1, 'TestAirport1', 'TestCity1', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// ...and inserting another line:
sqlite_cmd.CommandText = "INSERT INTO airports (id, airportName, airportCity, active) VALUES (2, 'TestAirport2', 'TestCity2', 1);";
// And execute this again ;D
sqlite_cmd.ExecuteNonQuery();
// But how do we read something out of our table ?
// First lets build a SQL-Query again:
sqlite_cmd.CommandText = "SELECT * FROM airports";
// Now the SQLiteCommand object can give us a DataReader-Object:
sqlite_datareader = sqlite_cmd.ExecuteReader();
// The SQLiteDataReader allows us to run through the result lines:
while (sqlite_datareader.Read()) // Read() returns true if there is still a result line to read
{
// Print out the content of the text field:
//System.Console.WriteLine(sqlite_datareader["text"]);
}
// We are ready, now lets cleanup and close our connection:
sqlite_conn.Close();
}
Everything is fine, database created, table created and some data inserted to table. (I check all with SQLite Manager on MozzilaFirefox)
My goal is to fill DataGrid on button click with data from database like this:
private void btnShow_Click(object sender, RoutedEventArgs e)
{
string database_connection = "Data Source=database.db;Version=3;New=True;";
string query = String.Format("SELECT * FROM airports WHERE active = '1'");
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
DataTable data = new DataTable();
dataAdapter.Fill(data);
dgAirports.DataContext = data;
}
But then visual studio throw exception that table 'airports' doesen't exist and for real, the whole database become empty.
How to fix that?
c# database wpf sqlite datagrid
c# database wpf sqlite datagrid
asked Nov 11 at 9:01
Nitrus Brio
1
1
2
What do you thinkNew=True;
does?
– mjwills
Nov 11 at 9:03
2
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13
add a comment |
2
What do you thinkNew=True;
does?
– mjwills
Nov 11 at 9:03
2
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13
2
2
What do you think
New=True;
does?– mjwills
Nov 11 at 9:03
What do you think
New=True;
does?– mjwills
Nov 11 at 9:03
2
2
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247224%2fhow-to-fill-datagrid-from-sqlite%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What do you think
New=True;
does?– mjwills
Nov 11 at 9:03
2
@mjwills I just followed Youtube tutorials for creating database and then just copied CQLiteConnection into button Show action. But now I get it. So I just needet to delete that part of the line. Thank's.
– Nitrus Brio
Nov 11 at 9:13