I have a C#.NET Website that stores status logs from hardware installations. A report is stored in a report_info table and it's data entries are stored in report_data. I first insert into the report_info table and retrieve the insert ID using:

cmd.CommandText = "SELECT @@IDENTITY";
// Get the last inserted id.
aReport.reportId = Convert.ToInt32(cmd.ExecuteScalar());

aReport.reportId is then used as the Foreign Key in report_data table's entries. I have debugged to show that the reportId is correct and exists in the table, however, I get the error: "The INSERT statement conflicted with the FOREIGN KEY constraint"

Any ideas on what might cause this error would be really helpful. I'm at the 'losing patience' point ...


6 Years
Discussion Span
Last Post by abarrett86

I figured it out. So it would work on the first time around. The report_info table and the report_data table have a reference to the installation table. I had the report_info_id and installation_id parameters flipped, so it would work the first time when the installation_id and the report_info_id were both 1, it would not work on subsequent inserts. Thanks

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.