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?










share|improve this question

















  • 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














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?










share|improve this question

















  • 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












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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 9:01









Nitrus Brio

1




1







  • 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












  • 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







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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3