unable to add data to table because related record is required
0
votes
1
answer
323
views
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:
With this code in C# with an OleDb... commands and connections Which are working fine for other query's:
INSERT INTO Tasks (Assigned,Project,Description) VALUES (@assign,@project,@description)
On this Structure: 
//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); Console.WriteLine(project_id); } con.Close(); Console.WriteLine("submit: " + project_id + " " + employee_id + " " + descriptionTextBox.Text + " " + monthCalendar1.SelectionStart.ToString("MM/DD/YYYY")); 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.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.
Asked by Chris Johnson
(1 rep)
Aug 6, 2018, 03:30 PM
Last activity: Apr 25, 2025, 07:07 AM
Last activity: Apr 25, 2025, 07:07 AM