SqlCommand returns 1 but database doesn't get updated.. what am i doing wrong?

226 views Asked by At
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand cmd = new SqlCommand("INSERT INTO Alarm (artistname, tijd) VALUES (@artistname, @tijd)", connection);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    cmd.Parameters.AddWithValue("@artistname", alarm.ArtistName);
    cmd.Parameters.AddWithValue("@tijd", alarm.Time);
    connection.Open();
    int a = cmd.ExecuteNonQuery();
    System.Windows.Forms.MessageBox.Show(a.ToString()); 
    // returns 1 but database doesn't actually update..
 }

The code above is what i'm using to update my database's Alarm table. int a returns a 1 but the database doesn't actually get updated. what am i doing wrong? visual studio doesn't give me any error messages either.

1

There are 1 answers

2
Jason Nowicki On

ExecuteNonQuery will return a 1 of successful. You are performing an INSERT statement not an UPDATE statement. If you want to the id of the new record that was added use

INSERT INTO Alarm (artistname, tijd) VALUES (@artistname, @tijd); 
Select Scope_Idenitity();

then use

int a = (int)cmd.ExecuteScalar();