I have an access database with my work that I am trying to insert into but I keep getting.

'You cannot add or change a record because a related record is required in table 'Projects'.'

I'm running this query: INSERT INTO Tasks (Assigned,Project,Description) VALUES (@assign,@project,@description)

On this Structure: enter image description here

With this code in C# with an OleDb... commands and connections Which are working fine for other query's:

//the assign id is already known and is of type integer.
            string query = "SELECT Project_ID FROM Projects WHERE Project_Name = @project";
            OleDbConnection con = new OleDbConnection(con_string);
            OleDbCommand cmd = new OleDbCommand(query, con);
            cmd.Parameters.AddWithValue("@project", projectComboBox.Text);
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
                project_id = Convert.ToInt16(reader[0]);


            Console.WriteLine("submit: " + project_id + " " + employee_id + " " + descriptionTextBox.Text + " " + monthCalendar1.SelectionStart.ToString("MM/DD/YYYY"));
            query = "INSERT INTO Tasks (Assigned,Project,Description) VALUES (@assign,@project,@description)";
            con = new OleDbConnection(con_string);
            cmd = new OleDbCommand(query, con);
            cmd.Parameters.AddWithValue("@project", project_id);
            cmd.Parameters.AddWithValue("@assign", employee_id);
            cmd.Parameters.AddWithValue("@description", descriptionTextBox.Text.ToString());
            //cmd.Parameters.AddWithValue("@deadline", monthCalendar1.SelectionStart);

I have tried looking at other examples of this problem and I don't understand why I'm getting this error. @project has a valid id number of the primary key for a Project, @assign has a valid employee id as well and @description is string of text. Thanks for any help.

  • Do you mean @project has a valid id number of the primary key for a Project -- you said @assign -- assuming cut and paste error here? Commented Aug 6, 2018 at 16:10
  • Yes I did, I meant to say that both '@project' and '@assign' both have the value of the primary keys for those tables. Commented Aug 6, 2018 at 16:12

1 Answer 1


The answer is that I was adding the parameters in the wrong order. All of your parameters have to line up in order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.