How to insert values to SQLite on button click (WPF)









up vote
-2
down vote

favorite












I'm trying to insert values on SQLite database with code like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format("INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('"+txtAirportId+"','"+txtAirportName+"','"+txtAirportCity+"','"+1+"')");
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
connection.Close();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();




And this is xaml code:



<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="Binding" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="Binding Path = id" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="Binding Path = airportName" Width="*"/>
<DataGridTextColumn Header="Град" Binding="Binding Path = airportCity" Width="*"/>
</DataGrid.Columns>
</DataGrid>

<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="Binding ElementName=dgAirports, Path=SelectedValue.id" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportName" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportCity" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>


<Button x:Name="btnShow" Content="Прикажи" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnShow_Click"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
</Grid>


So when I click on btnAdd nothing happening but I don't get any errors. So, what I'm missing and how to insert that values to 'airports' table?










share|improve this question























  • you created a command but didn't execute/run it
    – ASh
    Nov 11 at 19:56






  • 3




    Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
    – WelcomeOverflow
    Nov 11 at 19:58











  • @ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
    – Nitrus Brio
    Nov 11 at 20:00










  • Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
    – Shawn
    Nov 11 at 20:28















up vote
-2
down vote

favorite












I'm trying to insert values on SQLite database with code like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format("INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('"+txtAirportId+"','"+txtAirportName+"','"+txtAirportCity+"','"+1+"')");
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
connection.Close();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();




And this is xaml code:



<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="Binding" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="Binding Path = id" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="Binding Path = airportName" Width="*"/>
<DataGridTextColumn Header="Град" Binding="Binding Path = airportCity" Width="*"/>
</DataGrid.Columns>
</DataGrid>

<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="Binding ElementName=dgAirports, Path=SelectedValue.id" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportName" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportCity" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>


<Button x:Name="btnShow" Content="Прикажи" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnShow_Click"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
</Grid>


So when I click on btnAdd nothing happening but I don't get any errors. So, what I'm missing and how to insert that values to 'airports' table?










share|improve this question























  • you created a command but didn't execute/run it
    – ASh
    Nov 11 at 19:56






  • 3




    Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
    – WelcomeOverflow
    Nov 11 at 19:58











  • @ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
    – Nitrus Brio
    Nov 11 at 20:00










  • Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
    – Shawn
    Nov 11 at 20:28













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I'm trying to insert values on SQLite database with code like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format("INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('"+txtAirportId+"','"+txtAirportName+"','"+txtAirportCity+"','"+1+"')");
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
connection.Close();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();




And this is xaml code:



<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="Binding" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="Binding Path = id" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="Binding Path = airportName" Width="*"/>
<DataGridTextColumn Header="Град" Binding="Binding Path = airportCity" Width="*"/>
</DataGrid.Columns>
</DataGrid>

<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="Binding ElementName=dgAirports, Path=SelectedValue.id" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportName" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportCity" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>


<Button x:Name="btnShow" Content="Прикажи" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnShow_Click"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
</Grid>


So when I click on btnAdd nothing happening but I don't get any errors. So, what I'm missing and how to insert that values to 'airports' table?










share|improve this question















I'm trying to insert values on SQLite database with code like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format("INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('"+txtAirportId+"','"+txtAirportName+"','"+txtAirportCity+"','"+1+"')");
SQLiteCommand command = new SQLiteCommand(query, connection);
SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command);
connection.Close();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();




And this is xaml code:



<DataGrid x:Name="dgAirports" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="272" ItemsSource="Binding" AutoGenerateColumns="False" CanUserAddRows="false" VerticalScrollBarVisibility="Hidden">
<DataGrid.Columns>
<DataGridTextColumn Header="Шифра" Binding="Binding Path = id" Width="*"/>
<DataGridTextColumn Header="Назив" Binding="Binding Path = airportName" Width="*"/>
<DataGridTextColumn Header="Град" Binding="Binding Path = airportCity" Width="*"/>
</DataGrid.Columns>
</DataGrid>

<Label x:Name="lblAirportId" Content="ИД" HorizontalAlignment="Left" Margin="123,115,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportId" Text="Binding ElementName=dgAirports, Path=SelectedValue.id" HorizontalAlignment="Left" Height="23" Margin="157,115,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportName" Content="Назив" HorizontalAlignment="Left" Margin="109,142,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportName" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportName" HorizontalAlignment="Left" Height="23" Margin="157,143,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>

<Label x:Name="lblAirportCity" Content="Град" HorizontalAlignment="Left" Margin="115,168,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="txtAirportCity" Text="Binding ElementName=dgAirports, Path=SelectedValue.airportCity" HorizontalAlignment="Left" Height="23" Margin="157,171,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="125"/>


<Button x:Name="btnShow" Content="Прикажи" HorizontalAlignment="Left" Margin="10,115,0,0" VerticalAlignment="Top" Width="75" Click="btnShow_Click"/>
<Button x:Name="btnAdd" Content="Додај" HorizontalAlignment="Left" Margin="10,142,0,0" VerticalAlignment="Top" Width="75" Click="btnAdd_Click"/>
</Grid>


So when I click on btnAdd nothing happening but I don't get any errors. So, what I'm missing and how to insert that values to 'airports' table?







c# database wpf sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:44









Marco

45.5k10103131




45.5k10103131










asked Nov 11 at 19:55









Nitrus Brio

11




11











  • you created a command but didn't execute/run it
    – ASh
    Nov 11 at 19:56






  • 3




    Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
    – WelcomeOverflow
    Nov 11 at 19:58











  • @ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
    – Nitrus Brio
    Nov 11 at 20:00










  • Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
    – Shawn
    Nov 11 at 20:28

















  • you created a command but didn't execute/run it
    – ASh
    Nov 11 at 19:56






  • 3




    Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
    – WelcomeOverflow
    Nov 11 at 19:58











  • @ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
    – Nitrus Brio
    Nov 11 at 20:00










  • Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
    – Shawn
    Nov 11 at 20:28
















you created a command but didn't execute/run it
– ASh
Nov 11 at 19:56




you created a command but didn't execute/run it
– ASh
Nov 11 at 19:56




3




3




Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
– WelcomeOverflow
Nov 11 at 19:58





Never, ever glue data into a string to make a query - especially with SQLite. Always use SQL Parameters.
– WelcomeOverflow
Nov 11 at 19:58













@ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
– Nitrus Brio
Nov 11 at 20:00




@ASh But almost on the same way I have a button click action to fill DataGrid from database. Of course string query is diferent, but what I'm missing right now? How to execute it?
– Nitrus Brio
Nov 11 at 20:00












Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
– Shawn
Nov 11 at 20:28





Where does `database`.`airports` come from? (Use double quotes for identifiers if you feel the need to quote them, btw: "database"."airports") Your example doesn't ATTACH any database with a "database" schema name (And it's a bad name to use, since DATABASE is a keyword and using it as an identifier means you have to quote it, which looks ugly)
– Shawn
Nov 11 at 20:28













1 Answer
1






active

oldest

votes

















up vote
-1
down vote













Just change your function like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('txtAirportId ','txtAirportName ','txtAirportCity','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();



BTW: your code still will be not good. At least you will have SQL Injection vulnerability if you just contact string. Use Parameters or read about Entity Framework Core Technology.






share|improve this answer




















  • Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
    – Nitrus Brio
    Nov 11 at 20:07











  • post here full exception message pelase
    – Sergey Shulik
    Nov 11 at 20:08










  • An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
    – Nitrus Brio
    Nov 11 at 20:13






  • 1




    it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
    – Sergey Shulik
    Nov 11 at 20:15










  • Also, how would looks like querry for updateing? :)
    – Nitrus Brio
    Nov 11 at 21:22










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%2f53252628%2fhow-to-insert-values-to-sqlite-on-button-click-wpf%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-1
down vote













Just change your function like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('txtAirportId ','txtAirportName ','txtAirportCity','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();



BTW: your code still will be not good. At least you will have SQL Injection vulnerability if you just contact string. Use Parameters or read about Entity Framework Core Technology.






share|improve this answer




















  • Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
    – Nitrus Brio
    Nov 11 at 20:07











  • post here full exception message pelase
    – Sergey Shulik
    Nov 11 at 20:08










  • An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
    – Nitrus Brio
    Nov 11 at 20:13






  • 1




    it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
    – Sergey Shulik
    Nov 11 at 20:15










  • Also, how would looks like querry for updateing? :)
    – Nitrus Brio
    Nov 11 at 21:22














up vote
-1
down vote













Just change your function like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('txtAirportId ','txtAirportName ','txtAirportCity','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();



BTW: your code still will be not good. At least you will have SQL Injection vulnerability if you just contact string. Use Parameters or read about Entity Framework Core Technology.






share|improve this answer




















  • Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
    – Nitrus Brio
    Nov 11 at 20:07











  • post here full exception message pelase
    – Sergey Shulik
    Nov 11 at 20:08










  • An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
    – Nitrus Brio
    Nov 11 at 20:13






  • 1




    it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
    – Sergey Shulik
    Nov 11 at 20:15










  • Also, how would looks like querry for updateing? :)
    – Nitrus Brio
    Nov 11 at 21:22












up vote
-1
down vote










up vote
-1
down vote









Just change your function like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('txtAirportId ','txtAirportName ','txtAirportCity','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();



BTW: your code still will be not good. At least you will have SQL Injection vulnerability if you just contact string. Use Parameters or read about Entity Framework Core Technology.






share|improve this answer












Just change your function like this:



private void btnAdd_Click(object sender, RoutedEventArgs e)

string database_connection = "Data Source=database.db;Version=3;";
SQLiteConnection connection = new SQLiteConnection(database_connection);
connection.Open();

string query = String.Format($"INSERT INTO `database`.`airports`(`id`, `airportName`, `airportCity`, `active`) VALUES ('txtAirportId ','txtAirportName ','txtAirportCity','1')");
SQLiteCommand command = new SQLiteCommand(query, connection);
command.ExecuteNonQuery();

txtAirportId.Clear();
txtAirportName.Clear();
txtAirportCity.Clear();



BTW: your code still will be not good. At least you will have SQL Injection vulnerability if you just contact string. Use Parameters or read about Entity Framework Core Technology.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 20:00









Sergey Shulik

678824




678824











  • Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
    – Nitrus Brio
    Nov 11 at 20:07











  • post here full exception message pelase
    – Sergey Shulik
    Nov 11 at 20:08










  • An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
    – Nitrus Brio
    Nov 11 at 20:13






  • 1




    it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
    – Sergey Shulik
    Nov 11 at 20:15










  • Also, how would looks like querry for updateing? :)
    – Nitrus Brio
    Nov 11 at 21:22
















  • Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
    – Nitrus Brio
    Nov 11 at 20:07











  • post here full exception message pelase
    – Sergey Shulik
    Nov 11 at 20:08










  • An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
    – Nitrus Brio
    Nov 11 at 20:13






  • 1




    it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
    – Sergey Shulik
    Nov 11 at 20:15










  • Also, how would looks like querry for updateing? :)
    – Nitrus Brio
    Nov 11 at 21:22















Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
– Nitrus Brio
Nov 11 at 20:07





Now i get this error: An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll on command.ExecuteNonQuery(); line... Is there any way to execute query without creating separated function for executing?
– Nitrus Brio
Nov 11 at 20:07













post here full exception message pelase
– Sergey Shulik
Nov 11 at 20:08




post here full exception message pelase
– Sergey Shulik
Nov 11 at 20:08












An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
– Nitrus Brio
Nov 11 at 20:13




An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll Additional information: no such table: database.airports Even if there is database called 'database' and table called 'airports', obvious.
– Nitrus Brio
Nov 11 at 20:13




1




1




it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
– Sergey Shulik
Nov 11 at 20:15




it's related to your database schema and, probably, connection access right. Make sure that you have table airports in you database database (very wired name), and your connection have rights to write into this table.
– Sergey Shulik
Nov 11 at 20:15












Also, how would looks like querry for updateing? :)
– Nitrus Brio
Nov 11 at 21:22




Also, how would looks like querry for updateing? :)
– Nitrus Brio
Nov 11 at 21:22

















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%2f53252628%2fhow-to-insert-values-to-sqlite-on-button-click-wpf%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







這個網誌中的熱門文章

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

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto