Insert SQL NULL when no ComboBox value is selected
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开发者_如何学Python, 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 == NULL
in the table Student_Payment_Details
even 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.
One option is to have a 'NONE' currency record in your table and use that ID.
You have to take some distance from it and I see 2 options :
- Use a fake value (like the none from the other answer).
- Don't insert line at all and consider it as no option selected.
精彩评论