开发者

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
  1. Now , this currencyID is a foreign key in another table Student_Payment_Details.
  2. The currency table is hosted in a ComboBox Control.
  3. 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜