Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to pass null integer value into database, from front end code behind as parameter. So how can i pass null values can any one help me please?

C#
cmd.Parameters.Add("@HospitalId", SqlDbType.Int).Value = int.Parse(ddlHospital.SelectedValue.ToString());

I'm using this but i'm getting error on this cannot convert string value into in32


Posted
Updated 3-Aug-13 1:27am
v2
Comments
[no name] 3-Aug-13 7:29am    
If your field accepts null values you can try setting your parameter to DbNull.
Aboobakkar Siddeq D U 3-Aug-13 7:33am    
Ya my field allows null values, but how can i pass dbnull value? please help me

1 solution

You can use DBNull.Value[^] to set a null value.
C#
cmd.Parameters.Add("@HospitalId", SqlDbType.Int).Value = DBNull.Value;


You could also make the parameter of the stored procedure default to NULL[^], that way you would not need to set the parameter if no value is needed.
 
Share this answer
 
Comments
Aboobakkar Siddeq D U 3-Aug-13 7:37am    
Still getting same error
Failed to convert parameter value from a String to a Int32.

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