0

I am an amateur in C# but I tried to connect to SQL database in C# and I am using Visual Studio 2015.

Trying to Insert on button click:

private void button1_Click_1(object sender, EventArgs e)
{
    string hello = "Hello";
    //insert data to database.
    int rows;
    string query = "INSERT INTO MyTable (name) VALUES (@username)";
    try
    {
        using (myConnection = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand(query, myConnection))
        {
            myConnection.Open();
            cmd.Parameters.AddWithValue("@username", hello);
            rows =  cmd.ExecuteNonQuery();
            MessageBox.Show("ROWS AFFECTED: " + rows);
            myConnection.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

The table has only two columns: ID(AUTO_INCREMENT) and name(nchar(10))

Here is what I have tried: On button click, it shows:

Rows AFFECTED: 1

Which means there is no exception caught but When I click on "Show Table Data", the table is empty and it doesn't add a new record.

The Insert query works fine when I run it manually using query script.

The connection to the database is fine also as the SELECT query runs perfectly fine.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Mark
  • 121
  • 4
  • 13
  • 1
    Where are you clicking "Show Table Data"? Inside SSMS somewhere or is that a button in your app? –  Jun 03 '16 at 18:48
  • @Kalmino here: [link](http://s33.postimg.org/drvaewg7z/showtable.jpg) – Mark Jun 03 '16 at 18:51
  • @S.Akbari Just tried to change the database option "Copy to output directory" to "Copy if newer" but still insert doesnt work. – Mark Jun 03 '16 at 18:55
  • @Mark... Did you used `AttachDbFilename=|DataDirectory|\yourDB.mdf` in your `ConnectionString`? – Salah Akbari Jun 03 '16 at 18:57
  • @S.Akbari Yes I did – Mark Jun 03 '16 at 18:59
  • I guess your connection string may be pointed somewhere else. You could run a select in your code using the same connection immediately after the insert. – Jared Stroebele Jun 03 '16 at 18:59
  • 2
    @Mark...So I'm pretty sure that one of the option from duplicate answer should solve your issue. Please read that answer carefully and try all options for example: the databse that is updated is located in the subfolder **BIN\DEBUG** folder of your project. If you want to see the updated data just attach the database located in the bin/debug folder in ssms. Also make sure your table in server explorer is not already open, if it is already open you must refresh it to show updated data – Salah Akbari Jun 03 '16 at 19:01
  • @JaredStroeb I am using the same connectionString variable for both queries (SELECT and INSERT) works fine for SELECT but doesnt work for INSERT. – Mark Jun 03 '16 at 19:01
  • @S.Akbari Thankyou, sorry this may be a stupid question, how can I check if my VS2015 has SSMS installed already or do I have to install it seperately? – Mark Jun 03 '16 at 19:06
  • 2
    @Mark...You could attach the database located in Bin\Debug in server explorer in VS. – Salah Akbari Jun 03 '16 at 19:09
  • @S.Akbari Thankyou got it now. So how to apply all the changes of BIN/DEBUG database to my actual database file? – Mark Jun 03 '16 at 19:16

0 Answers0