1

My code:

 private void button1_Click(object sender, EventArgs e)
    {
        WriteToTable();
    } 

  public void WriteToTable()
    {
         string DBpath = @"Data Source=.\StudentInfoDB.db;Version=3;";
         using SQLiteConnection SQLConnection = new SQLiteConnection(DBpath);
         using var cmd = new SQLiteCommand(DBpath, SQLConnection);
         SQLConnection.Open();
         cmd.CommandText = "INSERT INTO Students (Name) VALUES ('Test')";
         cmd.ExecuteNonQuery();   
         SQLConnection.Close();
    }

In my database file, I have one table called Students and 3 columns called name, grade and phone number. I'm trying to ensure I correctly connected to the database so I decided I should try adding text under the Name column before I add anymore code.

I expected this code to output "Text" under the "Name" column to my database file, however after I press button1 nothing happens. No errors show up and no crashes occur.

I have tried debugging the code and searching online for common mistakes but sadly it did not help

  • It looks like your usage of [SQLLite](https://www.codeguru.com/dotnet/using-sqlite-in-a-c-application/) is correct. Do the other columns on the `Students` table allow null values? Try this post for more info about [profiling in SQLLite](https://stackoverflow.com/questions/3199790/is-there-a-tool-to-profile-sqlite-queries) to see if the SQL code is actually hitting the DB. – Tim Jarosz Dec 12 '22 at 16:26
  • @TimJarosz Hello, thanks for replying and I apologize for not responding quickly. I have `Not Null` unchecked to all columns. – Ahmed Sherif Dec 12 '22 at 18:38
  • Try to run the SQL command manually using the SQLLite Client: https://sqlite.org/cli.html#special_commands_to_sqlite3_dot_commands_ Does the table insert succeed? – Tim Jarosz Dec 12 '22 at 18:46
  • Apologies for the late reply. Yes, the table insert is succeeded. – Ahmed Sherif Dec 13 '22 at 13:11

0 Answers0