如果在ASP Classic中不是IsNull [英] If Not IsNull in ASP Classic

查看:82
本文介绍了如果在ASP Classic中不是IsNull的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ASP非常陌生,并且遇到语法错误,如果可以的话,我需要帮助.

I'm fairly new to asp and I've got a syntax error I would like help on if you can.

我有一个ASP页,其中显示了一个从sql中提取数据的表.尚未填充大多数数据,因此返回NULL.有问题的数据类型是数字.我需要FormatNumber rs,当它不为null时,如果不是,则不填充.

I've got an ASP page that shows a table that pulls data from sql. Most of the data hasn't been populated yet so returns a NULL. The data type in question is numeric. I need to FormatNumber the rs when it is not null and not populate if it is.

这就是我所拥有的

<%=If Not IsNull(rs("ContractValue")) Then FormatNumber(rs("ContractValue"),0) end if%>

但是如上所述,即时通讯收到语法错误.

But as mentioned, im getting a syntax error.

我在做什么错了?

推荐答案

在这种情况下,我建议不要使用IsNull(),而是先回答有关语法错误的问题.

I would recommend not using IsNull() in this scenario, but to answer the question about the syntax error first.

原因是<%= %>语法的缩写

<% Response.Write %>

在经典ASP中.

因此,如果不使用速记方法编写的内容,实际上是在做什么;

So what you are actually doing if written without the shorthand approach is;

<% Response.Write If Not IsNull(rs("ContractValue")) Then FormatNumber(rs("ContractValue"),0) End If %>

这是错误的语法,将触发Syntax Error.

which is incorrect syntax and will trigger a Syntax Error.

要修复代码,请像这样从<% %>标记中删除=

To fix the code remove the = from the <% %> tags, like so;

<% If Not IsNull(rs("ContractValue")) Then Response.Write FormatNumber(rs("ContractValue"),0) End If %>

使用IsNull怎么样?

尽管这可行,但由于DBNull (取决于所使用的数据库)可能不同,并且通常与VBScript vbNull变体不同,因此通常会产生奇怪的结果.

What about using IsNull?

While this can work it can often give weird results because a DBNull (depending on the database being used) can be different and is often different to the VBScript vbNull variant.

由于这个原因,而且VBScript的类型不是很强,我发现使用简单的快速强制转换字符串来避免Null,然后检查有效数据很有用.

Because of this and the fact VBScript isn't strongly typed I find it useful to use a simple quick cast to string to avoid Nulls then check for valid data.

数字校验示例

Dim contractValue
contractValue = rs("ContractValue") & ""
If Len(contractValue) > 0 And IsNumeric(contractValue) Then contractValue = Clng(contractValue) Else contractValue = 0

您可以通过编写一段可重用的代码来进一步实现此功能,该功能在本文中将对此功能进行说明.

You can take this further by writing a reusable piece of code that IIf() function explained in this post.

像这样的东西

Dim contractValue
contractValue = rs("ContractValue") & ""
contractValue = IIf(Len(contractValue) > 0 And IsNumeric(contractValue), contractValue, 0)

@Paul

@Paul made a good point about evaluation of parameters, in the original code would potentially break

contractValue = IIf(Len(contractValue) > 0 And IsNumeric(contractValue), Clng(contractValue), 0)

因为将评估Clng(contractValue)而不管结果是True还是False.因此,之后需要进行任何格式设置,或者构建IIf()函数的更复杂版本.

because Clng(contractValue) would be evaluated regardless of whether the outcome was True or False. So any formatting would need to be afterwards or a more complex version of the IIf() function be built.

这篇关于如果在ASP Classic中不是IsNull的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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