Quantcast
Channel: DataSource Controls - SqlDataSource, ObjectDataSource, etc.
Viewing all articles
Browse latest Browse all 956

Violation of foreign key constraint while inserting a new itemNo

$
0
0
CREATE TABLE Approvals (
  App_ID int NOT NULL IDENTITY,
  ItemNo int NOT NULL 
	REFERENCES IssueLog(ItemNo)
		ON UPDATE CASCADE
		ON DELETE NO ACTION,		 
  AppType int NOT NULL,
  ByWhom int DEFAULT NULL,
  ApprovalDate date DEFAULT NULL,      
  CONSTRAINT PK_Approvals PRIMARY KEY NONCLUSTERED ([App_ID], [ItemNo])      
); 
// get the generated item no                 
Int32 ItemNo = (Int32)e.Command.Parameters["@NewItemNo"].Value;

Approvals app = new Approvals(); 
  
SqlConnection conn = new SqlConnection();                
conn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=RCS_2_02;Data Source=localhost\\SQLEXPRESS";
SqlCommand command = new SqlCommand("insert into Approvals (ItemNo, AppType, ByWhom, ApprovalDate) VALUES (@ItemNo, @AppType, @ByWhom, @ApprovalDate)", conn);
command.Parameters.AddWithValue("@ItemNo", ItemNo);

// Enter Issue Log Type
command.Parameters.AddWithValue("@AppType", 1);
command.Parameters.AddWithValue("@ByWhom", Convert.DBNull);
command.Parameters.AddWithValue("@ApprovalDate", Convert.DBNull);


conn.Open();
command.ExecuteNonQuery();
conn.Close();

Should I avoid the cascade update clause on my create db statement?

Because it triggers a foreign key violation here when I am inserting a brand-new itemNo

Thanks

Jack


Viewing all articles
Browse latest Browse all 956

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>