StudeStude> Scenario – C# Application + SQL Server 2008 R2
I have a table Currency with the following structure
currencyID - int - NOT NULL - Primary Key - IS Identity (**True**)
currency - VARCHAR(50) - NOT NULL
- Now , this currencyID is a foreign key in another table Student_Payment_Details.
- The currency table is hosted in a ComboBox Control.
- I need a way to insert NULL (or nothing) in case no currency option is selected.
(e.g selectedIndex = -1)
Been reading articles stating that it’s against all SQL rules to insert a NULL for a foriegn key while its bound to a primary NOT NULL key in the master table
So, guys, i need your suggestions for a workaround. Thank you in advance.
-
Here’s a trimmed version of my stored procedure
CREATE PROCEDURE [dbo].[spStudentPaymentDetails]
-- PARAMETERS **** blah blah *** @currencyID int = null, @studentFees varchar(100) = null, **** blah blah *** AS BEGIN SET NOCOUNT ON; -- Add Student Details INSERT INTO Student_Payment_Details(****,currencyID,studentFees,****) values(****,@currencyID,@studentFees,****); SELECT SCOPE_IDENTITY(); END
It’s possible to insert a record with
CurrencyID == NULLin the tableStudent_Payment_Detailseven if it’s a FK to the Currency table.what is not possible is to insert a record with PK NULL in the currency table but this is not your case.