0

I am not the best with how Oledb works, but please can you help me out. I have an access database in which the code is connected to; the problem is after executing my CommandText, no errors occur but the database does not update.

I might have missed something, please don't be annoyed.

    private void parametersEditCreate() // Used within method below
    {
        dateTime = Convert.ToDateTime(dtpDateofBirth.Value.ToShortDateString());
        command.Parameters.AddWithValue("@forename", txtForename.Text);
        command.Parameters.AddWithValue("@surname", txtSurname.Text);
        command.Parameters.AddWithValue("@username", txtUsername.Text);
        command.Parameters.AddWithValue("@password", txtPassword.Text);
        command.Parameters.AddWithValue("@class", txtClass.Text);
        command.Parameters.AddWithValue("@dob", dateTime.ToString());
        command.Parameters.AddWithValue("@gender", txtGender.Text);
        command.Parameters.AddWithValue("@cf", txtCourseFavourite.Text);
    }

    private void createEditRecord(string ID) // If 'Edit' disable StudentID (Autoincrement) and ignore (string ID) parameter, otherwise update.
    {
        string query;
        connection.Open();
        if (Teacher.CreateOREdit == "Edit")
        {
            query = "UPDATE tblStudent SET [Forename]=@forename,[Surname]=@surname,[Username]=@username,[Password]=@password,[Class]=@class,[DateofBirth]=@dob,[Gender]=@gender,[Course Favourite]=@cf WHERE [StudentID]=@id";
            command = new OleDbCommand(query, connection);
            command.Parameters.AddWithValue("@id", txtStudentID.Text);
            parametersEditCreate(); 

        }
        else //Create - FOR STACKOVERFLOW, ignore else
        {
            query = "INSERT INTO tblStudent([Forename],[Surname],[Username],[Password],[Class],[DateofBirth],[Gender],[Course Favourite]) VALUES (@forename,@surname,@username,@password,@class,@dob,@gender,@cf)";
            command = new OleDbCommand(query, connection);
        }

        command.ExecuteNonQuery();
        connection.Close();
    }
  • `int rowsAffected = cmd.ExecuteNonQuery()` will tell you how many rows were affected. If it is non zero, see [Why saving changes to a database fails?](http://stackoverflow.com/q/17147249/1070452). A DataAdapter can be 'taught' to do most of that for you (without datatype conversions) – Ňɏssa Pøngjǣrdenlarp Sep 03 '16 at 13:38
  • Not for nothing, but you have gotten 8 answers on your 5 questions - (some from the [best available minds](http://stackoverflow.com/a/38464280/1070452)!). You should take a minute to accept the ones which work (click the checkmark) so they get removed from the UnAnswered List. You could (should) also upvote any Q or A (anywhere) which helps or informs you - this helps others find good answers. If you take the [Tour] it explains how SQ works in that regard. – Ňɏssa Pøngjǣrdenlarp Sep 11 '16 at 22:34

1 Answers1

0

You could try using these things

    string query;
    OleDBlDataAdaptor da;
    OleDBCommandBuilder cb;
    DataSet ds = new DataSet();