简体   繁体   中英

Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.DBNull'

param7[1].Value = tbLastName.Text.Length > 0 ? tbLastName.Text : DBNull.Value;

Why is this impossible, and any suggestion what else to try? If/else for 20 parameters is just not way.

The issue is that (as the error message indicates) the conditional expression needs either identical types on both branches, or else there needs to be an implicit conversion from one of the types to the other. In your case, there isn't one, and so you get the error. One quick fix would be cast one of the values to (object) (which is fine, since DbParameter.Value is of type object anyway.)

So this should work for you:

param7[1].Value = tbLastName.Text.Length > 0 ? tbLastName.Text : (object)DBNull.Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM