为什么我的布尔值没有被分配正确的值?

B. *_*non 5 vb.net asp.net html-select

我有这个 ASP.Net 代码:

<select name="subcategory" color="<%=Session("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
    <% if not IsNewBusiness then %>
      <option value="0">Existing
      <option value="1">Organic Growth
    <% else %>
      <option value="0">New
      <option value="1">Assumed
    <% end if %>
</select>
Run Code Online (Sandbox Code Playgroud)

...应该根据布尔值 IsNewBusiness 的值将一对项目中的一个或另一个加载到 html select 元素。

但是,相同的逻辑始终等于 true(“not IsNewBusiness”),并且前两项(“Existing”和“Organic Growth”)始终是填充 select 元素的那些。

赋值的代码是:

Dim IsNewBusiness As Boolean
. . .
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
End If
Run Code Online (Sandbox Code Playgroud)

正如下面查询结果中的“NewBiz”列所示,在某些情况下,该值应该为真(NewBiz 不为 0 时为真):

在此处输入图片说明

但即使我在我的站点中选择“-1”单元之一,我仍然在选择元素中得到“现有”和“有机增长”,而不是“新”和“假设”。

我的逻辑有问题吗?

更新

查询代码被达到。我知道这是因为我在那里放了一行假代码:

Wscript.Echo "Like this?" ' bogus
IsNewBusiness = TRUE 'default (if record not found)
Run Code Online (Sandbox Code Playgroud)

...当我运行它时,我得到:

"Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30451: Name 'Wscript' is not declared.

Source Error:



Line 127:        SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
Line 128:        adoRS.Open(SQLString, adoCon)
Line 129:        Wscript.Echo "Like this?"
Line 130:       IsNewBusiness = TRUE 'default (if record not found)
Line 131:        If Not adoRS.EOF Then     

Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 129 
Run Code Online (Sandbox Code Playgroud)

更新 2

由于逻辑总是等于假,我必须假设这一行:

IsNewBusiness = adoRS.Fields.Item(0).Value <> 0
Run Code Online (Sandbox Code Playgroud)

...正在到达,并且 adoRS.Fields.Item(0).Value 始终为 0

因此 IsNewBusiness 一开始就设置为 TRUE,但永远不会保持为真,尽管在某些情况下应该如此。

更新 3

现在有一些非常奇怪的事情 - 当我将年份的分配更改为明显虚假的分配时:

currentYear = 2525 'Year(Now)
Run Code Online (Sandbox Code Playgroud)

(表中没有当年的条目)

...我仍然得到相同的结果 - “现有”和“有机增长”是填充选择元素的两个值。

更新 4

当我添加 JonP 推荐的 Response.Writes 时,html 看起来不错:

在此处输入图片说明

...但在 VBScript 中似乎不需要:

在此处输入图片说明

事实上,当我运行它时,我得到了这个:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30800: Method arguments must be enclosed in parentheses.

Source Error:



Line 127:        SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
Line 128:        adoRS.Open(SQLString, adoCon)
Line 129:        Response.Write "<!-- Is New Biz = " . IsNewBusiness . "-->"    
Line 130:        IsNewBusiness = TRUE 'default (if record not found)
Line 131:        If Not adoRS.EOF Then


Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 129 
Run Code Online (Sandbox Code Playgroud)

至于在html中添加的那个,我可以在查看源代码中看到:

Response.Write "<!-- Is New Biz = " . IsNewBusiness . "-->" 
Run Code Online (Sandbox Code Playgroud)

……但这对我没有多大好处。该值不在控制台或我可以看到的任何其他地方...

更新 5

Jon P:当我尝试这样做时,就像这样:

<% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>
Run Code Online (Sandbox Code Playgroud)

... IsNewBusiness,在此处声明(在文件顶部):

<script language="VB" runat="Server">
. . .
Dim IsNewBusiness As Boolean
Run Code Online (Sandbox Code Playgroud)

...是红色的(表明它被编译器视为外来抗体),并且我收到运行时错误:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30800: Method arguments must be enclosed in parentheses.

Source Error:



Line 722:                       </td>
Line 723:                   </tr>
Line 724:                        <% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>
Line 725:                        <tr>
Line 726:                            <td nowrap align="left" valign="top">


Source File: C:\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 724 
Run Code Online (Sandbox Code Playgroud)

更新 6

建议使用语法调整:

<% Response.Write("<!-- Is New Biz = " . IsNewBusiness . "-->") %>
Run Code Online (Sandbox Code Playgroud)

...它仍然失败:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30456: 'IsNewBusiness' is not a member of 'String'.

Source Error:

Line 723:                   </tr>
Line 724:                        <%--<% Response.Write "<!-- Is New Biz before select elements assigned = " . IsNewBusiness . "-->" %>--%>
Line 725:                        <% Response.Write("<!-- Is New Biz = " . IsNewBusiness . "-->") %>
Line 726:                        <tr>
Line 727:                            <td nowrap align="left" valign="top">
Run Code Online (Sandbox Code Playgroud)

...可能是因为“IsNewBusiness”是一个布尔值,需要转换为字符串?

更新 7

我试过这个:

<% Response.Write("<!-- Is New Biz = " . CStr(IsNewBusiness) . "-->") %>
Run Code Online (Sandbox Code Playgroud)

...并得到,“ BC30456:'CStr' 不是 'String' 的成员。

我也试过:

<% Response.Write("<!-- Is New Biz = " . Convert.ToString(IsNewBusiness) . "-->") %>
Run Code Online (Sandbox Code Playgroud)

...并得到,“ BC30456:'Convert' 不是'String' 的成员。

更新 8

即使我把它放在一个我知道可以到达的地方:

<%
Response.Write("<!-- Is New Biz = " & CStr(IsNewBusiness) & "-->")
Unit = Request.QueryString.Item("Unit")
. . .
Run Code Online (Sandbox Code Playgroud)

...我什么也没看见。我不确定会发生什么 - “警报”?我按F12,控制台中没有任何内容......

B. *_*non 4

一旦我使用了 MCND 中的“调试消息”代码并将数据库代码移至“保存操作”上方,它就可以正常工作。这是在一个小上下文中:

<%
. . .   
'determine whether this unit is a new business
currentYear = Year(Now)
SQLString = "Select NewBiz from MasterUnitsprojSales where CYear = " & currentYear & " and Unit = '" & Unit & "'"
adoRS = New ADODB.Recordset
adoRS.Open(SQLString, adoCon)
IsNewBusiness = TRUE 'default (if record not found)
If Not adoRS.EOF Then
    IsNewBusiness = adoRS.Fields.Item(0).Value <> 0 
    Response.Write("<!-- IsNewBusiness after NOT EOF = " & CStr(IsNewBusiness) & "-->")
End If
adoRS.Close()

If Request.Form.Item("Action") = "Save" Then
. . .
Run Code Online (Sandbox Code Playgroud)