Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I created application, in which I want to store combobox value items into database.

Problem is if value member is null then pass 0, -1, etc in database. But this field has a foreign key. So gives error.

so, how can I store null value into integer field?


Thx.
Posted
Updated 14-Sep-12 1:12am
v3
Comments
Malli_S 14-Sep-12 7:14am    
You can have an entry for 0 or -1 in the table from where the column refers the foreign key.

Use NULL in the column definition[^] that makes up the foreign key constraint[^]. That way you can express that there are no records connected to the current record via this foreign key.

Modification:
In the stored proc, give the nullable parameters a default value of NULL,

and in your C# code, if no value is being passed to those parameters, simply don't attach them to your command.
Please read the whole thread as there is a lot of useful information there.
End Modification

Second Modification
MSDN[^] wrote:

Specifying the Order of Parameters


If you supply parameters in the form @parameter = value, you can supply them in any order. You can also omit parameters for which defaults have been supplied. If you supply one parameter in the form @parameter = value, you must supply all subsequent parameters this way. If you do not supply parameters in the form @parameter = value, you must supply them in the order given in the CREATE PROCEDURE statement.

When executing a stored procedure, the server rejects any parameters that were not included with the parameter list during procedure creation. Any parameter passed by reference (explicitly passing the parameter name) is not accepted if the parameter name does not match.

This explains how to pass the parameters by specifying them by name and in any order you like. Missing parameters that have a default value of NULL will thus be set to NULL.
End Second Modification

Regards,

— Manfred
 
Share this answer
 
v3
Comments
[no name] 14-Sep-12 7:41am    
if manually insert null value then its work, but in Windows form side not works.
Manfred Rudolf Bihy 14-Sep-12 7:51am    
If you have the definition set up as recommended and manual insertion via INSERT statement works. Then you should be able to do this from code also. Can you post the relevant bits from your code and your stored procedure? I think that would really help us clear this up for you. :)
[no name] 14-Sep-12 7:58am    
ya, if i dont pass parameter then by default save as Null,

but dont kw how to do with parameter?
Manfred Rudolf Bihy 14-Sep-12 8:06am    
Make the default value of the stored procedures parameter that is used to pass in the int NULL. Then when you call the stored procedure don't add that parameter to your statement. Then it will be set to null as the default. When you have changed your SP please test it first from SQL Management Studio or whatever you're using to make sure the mechanism works as expected.
Manfred Rudolf Bihy 14-Sep-12 8:10am    
I've added some content to my solution that will help you in passing the parameters in a way that will NULL them if desired.
how to pass default value to date parametere in storedprocedure - Sqlserver 2008?
http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008[^]
 
Share this answer
 
v2
http://stackoverflow.com/questions/15540932/how-to-pass-default-value-to-date-parametere-in-storedprocedure-sqlserver-2008]
 
Share this answer
 
tbl_Main               tbl_Sub
---------------------------------
Col1     -------->     Col1
pk                     fk
Not Allow null         Allow Null


now,when combobox's selected value is 0 or -1 pass NULL value for tbl_sub's Col1(Foreign key)

Happy Coding!
:)
 
Share this answer
 
v2
Comments
[no name] 14-Sep-12 7:37am    
i do this way but,
i use Sp to insert, in form side when pass array of arguments then its take 0 so, reference field not contain 0 and give error.
Aarti Meswania 14-Sep-12 7:41am    
have u set foreign-key allow null = true?
and do not insert '0' insert NULL

your combobox's values, like this?
0 --select--
1 value-1
2 value-2
...

[no name] 14-Sep-12 7:47am    
and 0 is not allow to insert because of reference!!!
Aarti Meswania 14-Sep-12 7:48am    
what value 0 contains?
[no name] 14-Sep-12 7:45am    
i dont know how to insert null??

how can store null into integer Parameter which is pass to the Store Procedure!!!
Hi,

If you want to insert NULL in to the database table column, remove all the contraints like Primary Key, Foriegn Key and Composite Key constraints associated with that column of a table. Basically, if you have constaints then the value is referring to value in some other table, since the server could not map NULL value to other table, it is throwing the error. So, remove the constraints of that column and then you can insert NULL value to that.

hope it works.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 14-Sep-12 7:27am    
Sorry, that is so wrong on so many levels, I had to give you a one vote.
Removing primary keys and foreing key constraints, just to have a foreign key point at no connected records. WTF!
You don't go and break referential integrity and the assurance of having a unique primary key to achieve what OP wants. All you have to do is set the column that makes up the foreign key definition as nullable using the NULL keyword. This has been in place since DBs exist as there has always been a need to be able to model a relationsip 1 : 0 .. N.

Cheers!
fjdiewornncalwe 14-Sep-12 9:30am    
Agreed. "that is so wrong on so many levels"
[no name] 14-Sep-12 7:39am    
agree 3762091

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900