0

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: picture of database structure in access

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

private void addTaskBTN_Click(object sender, EventArgs e)
        {
            //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);
            con.Open();
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                project_id = Convert.ToInt16(reader[0]);
                Console.WriteLine(project_id);
            }
                
            con.Close();

            Console.WriteLine("submit: " + project_id + " " + employee_id + " " + descriptionTextBox.Text + " " + monthCalendar1.SelectionStart);
            Console.WriteLine(monthCalendar1.SelectionStart);
            query = "INSERT INTO Tasks (Assigned,Project,Description) VALUES (@assign,@project,@description)";
            con = new OleDbConnection(con_string);
            cmd = new OleDbCommand(query, con);
            cmd.Parameters.Clear();
            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);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            this.Close();
        }

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.

  • Whatever `project_id` you are inserting does not exist in the `Project` table, it needs to before you can add it to `Tasks` (there is a foreign key constraint). Try debugging to see what the actual value being inserted is. Add those values to the question. – maccettura Aug 06 '18 at 18:19
  • 1
    This is a classic. You add the parameters in the wrong order. Your _project_id_ values is used for the Assigned field, and the _assigned_ field receives the _project_id_ value. – Steve Aug 06 '18 at 18:28
  • OleDb is not able to shuffle the parameters collection looking at the parameter's name. It blindly set the parameters in the same order in which you add them – Steve Aug 06 '18 at 18:29
  • Wow thank you so much! I feel terrible for the question but now I hope that everyone can learn from it! – Chris Johnson Aug 06 '18 at 18:44

1 Answers1

0

Steve correctly identified the mistake you have to put your parameters in the right order. My fix was to arrange my parameters in order.

  • As I have said in comments. This problem is a classical one. There are many duplicates of. There is no point in adding another answer to this. – Steve Aug 06 '18 at 19:17