0

The following code, is supposed to add a new record to the table 'Lista'. The code runs without any problems, but does not reflect in the table. The table contains 3 columns, Id (Is Identity is set to true), Color (nvarchar(50)) and Tela (nvarchar(50)).

I have tried and tried to figure this out, but no luck. I have also set the App.config File property, 'Copy to Output Directory' to 'Copy if Newer'. Please, help!

private void SaveLista()
{
    String ConString = ConfigurationManager.ConnectionStrings["WindowsFormsApp33.Properties.Settings.Testbase1ConnectionString"].ConnectionString;
    SqlConnection connection;

    String query = "INSERT INTO Lista ([Color], [Tela]) VALUES (@Color, @Tela)";

    using (connection = new SqlConnection(ConString))
    using (SqlCommand command = new SqlCommand(query, connection))
    {
        connection.Open();

        command.Parameters.AddWithValue("@Color", txtColor.Text);
        command.Parameters.AddWithValue("@Tela", txtTela.Text);

        int i = Convert.ToInt32(command.ExecuteScalar());
        MessageBox.Show(i.ToString());
    }
}

The result should be a new record in the table containing the values for Color and Tela which are in the TextBoxes on the form. The Id, of course, is filled automatically. Instead, the table remains empty! Thanks for any help!

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Armando
  • 11
  • 2
  • 1
    Please show your connectionstring – Steve Feb 18 '19 at 13:40
  • 2
    What does the connection string look like?, Its 99% likely that your simply looking at the wrong database. – Alex K. Feb 18 '19 at 13:40
  • 3
    And, you should use ExecuteNonQuery, albeit also ExecuteScalar should execute the query – Steve Feb 18 '19 at 13:42
  • connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Testbase1.mdf;Integrated Security=True" – Armando Feb 18 '19 at 13:48
  • @Alex K.: But wouldn't that raise an error of sorts? Because I do not have any other table with the same structure (column names and so on). – Armando Feb 18 '19 at 13:55
  • Did you check the database in the BIN\DEBUG folder? – Steve Feb 18 '19 at 13:55
  • @Steve: I don't know if its what you need, but I pasted the connection string from the App.config. I hope that is what you are referring to. – Armando Feb 18 '19 at 13:56
  • Yes exactly. That connectionstring will force VisualStudio to copy your mdf in the BIN\DEBUG folder while you are running your program. Changes should be there – Steve Feb 18 '19 at 13:57
  • More here https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails – Steve Feb 18 '19 at 13:58
  • Armando the lack of an error (assuming your not swallowing exceptions further up the stack) means the database was located, opened, queried & updated without any problem so this issue is most likely a confusion relating to the location of the mdf your looking at and the location the code is using as a consequence of the build process. – Alex K. Feb 18 '19 at 14:05
  • I am not as proficient as I would like to be at this. I get what the problem is now. I saw that the Database in the Bin/debug folder updates (saw the time stamp). What changes should I do in the App.config? – Armando Feb 18 '19 at 14:16
  • Nothing. This is how it supposed to behave when you debug your app through Visual Studio. When you deliver your app to your customers the MDF file stays in the same folder of your app. I suggest just to add the _;Database=MyLogicalDBName;_ to your app.config to give your db a logicalname – Steve Feb 18 '19 at 14:39

0 Answers0