Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
cmd.Parameters.AddWithValue("@FIRST_NAME", string.IsNullOrWhiteSpace(FIRST_NAME) ? DBNull.Value : FIRST_NAME);

Error:
no implicit conversion between 'system.dbnull' and 'string'
Posted

... or if it's a DBNull.Value you need (instead of the null value solution above), try:

C#
cmd.Parameters.AddWithValue("@FIRST_NAME", string.IsNullOrWhiteSpace(FIRST_NAME) ? (object)DBNull.Value : (object)FIRST_NAME);


The conversion to (object) avoids the "no implicit conversion between 'system.dbnull' and 'string'" exception.
 
Share this answer
 
Comments
sathish kumar 21-Oct-13 9:40am    
The one you mentioned is working it inserting null value into database, but my problem is it's insert null value to at all times.

It must check the textbox and should enter null only when it is empty
hypermellow 21-Oct-13 9:45am    
Ok, try this (assuming your textbox is called txtFIRSTNAME):

cmd.Parameters.AddWithValue("@FIRST_NAME", (string.IsNullOrWhiteSpace(txtFIRSTNAME.Text)) ? (object)DBNull.Value : (object)txtFIRSTNAME.Text);
Try this
cmd.Parameters.AddWithValue("@FIRST_NAME", string.IsNullOrWhiteSpace(FIRST_NAME) ? DBNull.Value : FIRST_NAME);
 
Share this answer
 
v2
Comments
sathish kumar 21-Oct-13 9:43am    
Sorry, I got this error

INSERT_NEW_STARTER_DETAILS' expects parameter '@FIRST_NAME', which was not supplied.
Ranjan.D 21-Oct-13 10:14am    
Could you verify the column is NULLable in database table ? Just a second quick check.

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