ASP.net #c 与 null 比较不起作用
Artwork.ArtworkMyAvailableVotesDataTable dtCommon2 = new Artwork.ArtworkMyAvailableVotesDataTable();
using (ArtworkTableAdapters.ArtworkMyAvailableVotesTableAdapter artworkTemplates = new ArtworkTableAdapters.ArtworkMyAvailableVotesTableAdapter())
{
artworkTemplates.Fill(dtCommon2, Master.loginData.loggedInUser.ID);
}
for (int i = 0; i < dtCommon2.Count; i++)
{
string voteStatus;
if (dtCommon2[i].isApproved == System.DBNull.Value)
{
isApproved 返回 null true 或 false...但我尝试的任何操作似乎都无法比较它是否为 null,它不会构建。
错误 1 运算符 '==' 不能 应用于“bool”类型的操作数和 'System.DBNull'
Artwork.ArtworkMyAvailableVotesDataTable dtCommon2 = new Artwork.ArtworkMyAvailableVotesDataTable();
using (ArtworkTableAdapters.ArtworkMyAvailableVotesTableAdapter artworkTemplates = new ArtworkTableAdapters.ArtworkMyAvailableVotesTableAdapter())
{
artworkTemplates.Fill(dtCommon2, Master.loginData.loggedInUser.ID);
}
for (int i = 0; i < dtCommon2.Count; i++)
{
string voteStatus;
if (dtCommon2[i].isApproved == System.DBNull.Value)
{
The isApproved returns null true or false... but nothing I try seems to work to compare if it's null, it wont build.
Error 1 Operator '==' cannot be
applied to operands of type 'bool' and
'System.DBNull'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来
isApproved
是普通的布尔值..无论如何,如果它是bool?
你可以使用这样的代码来保证安全:编辑:你没有使用原始值可以的普通 DataTable确实是 DBNull ..我猜您正在使用的组件已经在处理这个问题并为您提供“准备使用”的数据类。因此,如果
isApproved
是布尔值,那么当字段为空时它就已经是 false。Looks like
isApproved
is ordinary boolean.. anyway if it'sbool?
you can play it safe with such code:Edit: You're not using ordinary DataTable where the raw value can be indeed DBNull.. I guess the component you're using is dealing with this already and present you with "ready to use" class of data. So if
isApproved
is boolean it will already be false when the field will be empty.错误明确指出 isApproved 是 bool 类型,而不是 bool?或其他任何东西。如果是这种情况,我建议你检查一下程序的逻辑。也许你必须将其声明为 bool ?在 ArtworkMyAvailableVotesDataTable 上,但我不确定这是一个选项。
The error clearly states isApproved is a bool type, not bool? or anything else. If this is the case, I suggest you to review the logic of the program. Maybe you must declare it as bool? on ArtworkMyAvailableVotesDataTable, but I'm not sure that's an option.
您收到此错误是因为我们无法将 Null 分配给 bool。要么需要将 IsApproved 设置为 Nullable 布尔值,可以通过声明 bool 来实现吗?已获批准。或者您需要像 dt[i] != null 一样进行检查。
更新:尝试这个 Convert.IsDBNull(object) 检查是否为空。您还可以使用 Convert.ToBoolean(object)。如果对象为 null,则返回 false。
You are getting this error because we cannot assign Null to bool. Either you need to make IsApproved as Nullable boolean, can be achieved by declaring like bool? IsApproved. Or you need to check like dt[i] != null.
UPDATE: Try this Convert.IsDBNull(object) check whether is null or not. You can also use Convert.ToBoolean(object). It will return false, if the object is null.