1

I want to insert data into sqlite database in c#. I am using WinForms. I got an Exception FileLoadException.

SQLiteConnection con = new SQLiteConnection(@"Data Source=c:\\sqlite\\mySqliteDb.sqlite;Version=3;New=True;Compress=True;");

private void InsBtn_Click(object sender, EventArgs e)
{
   try
   {
      con.Open();
      string Query = "insert into sqliteDb(ID,Name) values('"+this.textBox1.Text + "','" + this.textBox2.Text + "')";

      SQLiteCommand cmd = new SQLiteCommand(Query,con);
      cmd.ExecuteNonQuery();
      con.close();
   }
   catch (Exception ex) 
   {
      MessageBox.Show(ex.Message);
   }
}
Artem Kulikov
  • 2,250
  • 19
  • 32
  • On which line throws this exception? What are the column types in your `sqliteDb` table? And you should always use [parameterized queries](http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/). This kind of string concatenations are open for [SQL Injection](http://en.wikipedia.org/wiki/SQL_injection) attacks. – Soner Gönül Aug 13 '15 at 07:13

1 Answers1

0

You should insert values into a table, not directly into the database.

Suppose there is a table:

Create table TbTexts(val1 text, val2 text)

Then

string Query = "insert into TbTexts(val1, val2) values('"+this.textBox1.Text + "','" + this.textBox2.Text + "')";
tafia
  • 1,512
  • 9
  • 18