无法确定的条件前pression的类型,因为有'字符串'和'System.DBNull'之间的隐式转换 [英] Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'System.DBNull'

查看:390
本文介绍了无法确定的条件前pression的类型,因为有'字符串'和'System.DBNull'之间的隐式转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是为什么不可能的,任何建议还有什么尝试?的if / else 20的参数是不的方式。

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

推荐答案

的问题是,(因为错误信息表明)条件前pression需要在两个分支可以相同类型,否则需要有一个从类型到另一个隐式转换。你的情况,没有一个,所以你的错误。一个快速的解决将施放的一个值,以(目标)(这是很好的,因为 DbParameter.Value 是类型对象反正。)

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;

这篇关于无法确定的条件前pression的类型,因为有'字符串'和'System.DBNull'之间的隐式转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆