Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am using C# 4.0 at cmd.ExecuteNonQuery(); there is an error with the help of SQL server profile i saw that :

SQL
cmd.Parameters.Add("@employeeSocialTitle", SqlDbType.Char, 5).Value = employeeSocialTitle;


when employeeSocialTitle is null it replace it by default
and that makes error so how can i force it to insert null when the employeeSocialTitle value is null

regards
Posted
Comments
VJ Reddy 9-May-12 1:33am    
Thank you for accepting the solution.

1 solution

I think since the SQL server expects DBNull.Value instead of null, when employeeSocialTitle is null, it is replaced with default. To avoid this the null coalescing operator can be used as follows:
C#
cmd.Parameters.Add("@employeeSocialTitle", SqlDbType.Char, 5).Value = employeeSocialTitle ?? DBNull.Value;
 
Share this answer
 
v2

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